我是Python和Django的新手,并试图找到正确的方法来建立基本的Django项目以开始学习它。
以下是我的Python,Pip和Django版本详细信息-
查找版本的命令-
python --version
pip --version
python -m django --version
我使用以下命令在其中创建了一个项目和一个模块- django-admin startproject djangoCrud cd djangoCrud / python manage.py startapp api
我能够使用以下命令运行项目-
python manage.py runserver
然后我读到我将需要一个虚拟环境来进行进一步的开发,
为此,我使用以下命令来创建和运行它-
pip install virtualenv
virtualenv env
. env/bin/activate
但是当我在激活环境后尝试运行manage.py文件时,
引发错误-
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?
有人可以指导我怎么了,怎么解决?
尝试步骤-
pip install virtualenv
virtualenv env
. env/bin/activate
pip install Django
django-admin startproject projectNameHere
python manage.py startapp moduleNameHere
python manage.py runserver
能够解决有需要的人的正确顺序-
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ls
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ python --version
Python 3.7.4
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ pip --version
pip 19.2.3 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ python -m django --version
2.2.4
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ pip install virtualenv
Requirement already satisfied: virtualenv in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (16.7.2)
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ls
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ virtualenv env
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/bin/python3
Also creating executable in /Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/bin/python
Installing setuptools, pip, wheel...
done.
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ls
env
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ source env/bin/activate
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ls
env
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ python --version
Python 3.7.4
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ pip --version
pip 19.2.3 from /Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/pip (python 3.7)
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ python -m django --version
/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/bin/python3: No module named django
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ pip install Django
Collecting Django
Using cached https://files.pythonhosted.org/packages/d6/57/66997ca6ef17d2d0f0ebcd860bc6778095ffee04077ca8985928175da358/Django-2.2.4-py3-none-any.whl
Collecting sqlparse (from Django)
Using cached https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Collecting pytz (from Django)
Using cached https://files.pythonhosted.org/packages/87/76/46d697698a143e05f77bec5a526bf4e56a0be61d63425b68f4ba553b51f2/pytz-2019.2-py2.py3-none-any.whl
Installing collected packages: sqlparse, pytz, Django
Successfully installed Django-2.2.4 pytz-2019.2 sqlparse-0.3.0
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ python -m django --version
2.2.4
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ django-admin startproject projectNameHere
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ld
ld: warning: No version-min specified on command line
ld: no object files specified
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ l
bash: l: command not found
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ls
env projectNameHere
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ cd projectNameHere/
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ ls
manage.py projectNameHere
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ python manage.py startapp moduleNameHere
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ ls
manage.py moduleNameHere projectNameHere
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
August 31, 2019 - 06:48:23
Django version 2.2.4, using settings 'projectNameHere.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[31/Aug/2019 06:48:42] "GET / HTTP/1.1" 200 16348
^C(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ ls
db.sqlite3 manage.py moduleNameHere projectNameHere
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ manage.py makemigrations
bash: manage.py: command not found
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ python manage.py makemigrations
No changes detected
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
(env) Aniruddhas-MacBook-Pro:projectNameHere aniruddhanarendraraje$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
August 31, 2019 - 06:49:59
Django version 2.2.4, using settings 'projectNameHere.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[31/Aug/2019 06:50:06] "GET / HTTP/1.1" 200 16348
答案 0 :(得分:4)
创建virtualenv时,必须再次设置python。
首先在virtualenv中安装django:
source env/bin/activate
pip install django
如果您还有其他软件包,也必须安装。
virtualenv是一个单独的环境,必须自行设置。
答案 1 :(得分:1)
您需要在虚拟环境中安装Django。
答案 2 :(得分:1)
我认为,这对您有帮助: VSCode: https://code.visualstudio.com/docs/python/tutorial-django https://docs.djangoproject.com/en/2.2/intro/tutorial01/
答案 3 :(得分:1)
首先确保已安装python
和pip
。我建议安装python3。
然后通过pip安装python django。
然后pip3 install virtualenv
使用virtualenv -p python3 <env_name>
使用source <env_name>/bin/activate
激活环境
然后,使用django-admin startproject <project-name>
然后您可以进行迁移。
python3 manage.py makemigrations
python3 manage.py migrate
完成后,您可以继续以python manage.py createsuperuser <username>
身份创建超级用户
您可以通过其文档https://www.djangoproject.com/start/
进行详细描述PS:-如果python3是defautl版本,则python3和python2不会有区别,因为python命令将被符号链接到/ usr / bin / python3