我是Python的新手,我刚开始学习Django教程。我得到了创建虚拟环境,在其中安装Django,然后在其旁边设置我的项目结构的概念。但是然后,我一定搞砸了,因为尽管我刚刚安装了Django,但我的“ python manage.py migration”命令返回了一个错误,即找不到名为Django的模块。这是我所做的:
PS D:\f drive\KK\Projects\Git\python\Dev> virtualenv kk_env
Using base prefix 'c:\\program files\\python37'
New python executable in D:\f drive\KK\Projects\Git\python\Dev\kk_env\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
PS D:\f drive\KK\Projects\Git\python\Dev> pipenv --python 3.7 install django==2.2
Virtualenv already exists!
Removing existing virtualenv…
Creating a virtualenv for this project…
Pipfile: D:\f drive\KK\Projects\Git\python\Dev\Pipfile
Using C:/Python/Python37-32/python.exe (3.7.4) to create virtualenv…
[= ] Creating virtual environment...Already using interpreter C:\Python\Python37-32\python.exe
Using base prefix 'C:\\Python\\Python37-32'
New python executable in C:\Users\karthik\.virtualenvs\Dev-g5SlP1CQ\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter C:/Python/Python37-32/python.exe
Successfully created virtual environment!
Virtualenv location: C:\Users\karthik\.virtualenvs\Dev-g5SlP1CQ
Creating a Pipfile for this project…
Installing django==2.2…
Adding django to Pipfile's [packages]…
Installation Succeeded
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Success!
Updated Pipfile.lock (f002eb)!
Installing dependencies from Pipfile.lock (f002eb)…
================================ 3/3 - 00:00:01
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
PS D:\f drive\KK\Projects\Git\python\Dev> pipenv shell
Launching subshell in virtual environment…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS D:\f drive\KK\Projects\Git\python\Dev> deactivate
PS D:\f drive\KK\Projects\Git\python\Dev> kk_env/Scripts/activate
(kk_env) PS D:\f drive\KK\Projects\Git\python\Dev> django-admin startproject dj_site
(kk_env) PS D:\f drive\KK\Projects\Git\python\Dev> mv dj_site dj_root
(kk_env) PS D:\f drive\KK\Projects\Git\python\Dev> cd dj_root
(kk_env) PS D:\f drive\KK\Projects\Git\python\Dev\dj_root> ls
Directory: D:\f drive\KK\Projects\Git\python\Dev\dj_root
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/27/2019 12:12 AM dj_site
-a---- 9/27/2019 12:12 AM 648 manage.py
(kk_env) PS D:\f drive\KK\Projects\Git\python\Dev\dj_root> python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 16, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
从我的PowerShell的历史中可以明显看出,我遵循两个不同教程中的约定,并且我怀疑这与搞错了有关(我的最佳猜测是使用“ pipenv” vs“ pip”安装Django。但这构成了我的第二个问题。
我现在有两个问题:
如何解决这种情况? (如果有帮助,我还将附上我的项目结构的屏幕截图)
在上一次尝试中(n次尝试之后),除了使用'pip install“ django> = 2.2,<3”'来安装Django外,我几乎遵循了相同的步骤。一切正常,除了在安装过程中未创建“ pipfile”或“ pipfile.lock”。我模糊的理解是,pipfile是项目可移植性所必需的,我真的很想在做基础知识的同时正确使用它。是(缺少pipfile)是预期的还是我最好从事园艺工作?
答案 0 :(得分:0)
好像您处在错误的虚拟环境中,并且您留下了一个创建的pipenv并转到了另一个未安装Django的pipenv。
您可能是正确的,这与以下两个过程有关。相反,您可能会发现仅使用pipenv会更容易。
要点击,只需删除目录并启动一个新的powershell窗口即可。
然后,假设您已经安装了pipenv(看起来像是这样做的),那么您真正需要的就是这个:
pipenv install django
pipenv shell
django-admin startproject dj_site
cd dj_site
python manage.py migrate
...
答案 1 :(得分:0)
您的第一个命令-
PS D:\f drive\KK\Projects\Git\python\Dev> virtualenv kk_env
正在您的pwd
中创建虚拟环境。
您的第二个命令-PS D:\f drive\KK\Projects\Git\python\Dev> pipenv --python 3.7 install django==2.2
正在C:\Users\karthik\.virtualenvs\Dev-g5SlP1CQ
创建一个虚拟环境,从输出语句可以明显看出-
Successfully created virtual environment!
Virtualenv location: C:\Users\karthik\.virtualenvs\Dev-g5SlP1CQ
现在,您正在上述虚拟环境中而不是在kk_env
中安装Django。这可能是您收到问题中所述错误的原因。
您可以按照出现的顺序运行以下命令,以实现所需的功能!
virtualenv kk_venv
kk_env/Scripts/activate
pip install django
希望获得帮助!