windows python虚拟环境创建Django项目
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows python虚拟环境创建Django项目相关的知识,希望对你有一定的参考价值。
为虚拟环境新建一个目录。
创建一个目录将其命名为 virtualenv,再在终端中切换到这个目录,
先安装virtualenv
如有则跳过
pip install --user virtualenv
可以使用如下指定源:pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
在终端中切换到目录
切换到目录virtualenv;
C:\\Users\\Xiao>D:
D:\\>CD D:\\Pythonwork\\virtualenv
D:\\Pythonwork\\virtualenv>
创建Django虚拟环境
并像下面这样创建一个虚拟环境:
D:\\Pythonwork\\virtualenv>virtualenv django2.2.12_learn
created virtual environment CPython3.7.0.final.0-64 in 1015ms
creator CPython3Windows(dest=D:\\Pythonwork\\virtualenv\\django2.2.12_learn, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\\Users\\Xiao\\AppData\\Local\\pypa\\virtualenv)
added seed packages: pip==22.2.2, setuptools==65.3.0, wheel==0.37.1
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
D:\\Pythonwork\\virtualenv>
生成目录如下:
激活虚拟环境
建立虚拟环境后,需要使用下面的命令激活它:
D:\\Pythonwork\\virtualenv>django2.2.12_learn\\Scripts\\activate
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>
虚拟环境中安装Django
==指定版本,-i指定源
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>pip install Django==2.2.12 -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting Django==2.2.12
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/af/d1/903cdbda68cd6ee74bf8ac7c86ffa04b2baf0254dfd6edeeafe4426c9c8b/Django-2.2.12-py3-none-any.whl (7.5 MB)
Collecting pytz
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/85/ac/92f998fc52a70afd7f6b788142632afb27cd60c8c782d1452b7466603332/pytz-2022.6-py2.py3-none-any.whl (498 kB)
Collecting sqlparse
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/97/d3/31dd2c3e48fc2060819f4acb0686248250a0f2326356306b38a42e059144/sqlparse-0.4.3-py3-none-any.whl (42 kB)
Installing collected packages: pytz, sqlparse, Django
Successfully installed Django-2.2.12 pytz-2022.6 sqlparse-0.4.3
[notice] A new release of pip available: 22.2.2 -> 22.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>
这时在D:\\Pythonwork\\virtualenv\\django2.2.12_learn\\Lib\\site-packages可以看到Django包
Django中创建项目
在依然处于活动的虚拟环境的情况下(django2.2.12_learn包含在括号内) , 执行如下命令来新建一个项目(最后有个空格和.),可以自行测试带.和不带.的区别:
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>django-admin startproject siteA .
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>dir
2022/11/30 16:29 <DIR> django2.2.12_learn
2022/11/30 16:38 646 manage.py
2022/11/30 16:38 <DIR> siteA
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>dir siteA
2022/11/30 16:38 3,206 settings.py
2022/11/30 16:38 768 urls.py
2022/11/30 16:38 403 wsgi.py
2022/11/30 16:38 0 __init__.py
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>
创建数据库
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>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
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>dir
2022/11/30 16:44 131,072 db.sqlite3
2022/11/30 16:29 <DIR> django2.2.12_learn
2022/11/30 16:38 646 manage.py
2022/11/30 16:44 <DIR> siteA
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>
查看运行项目
python manage.py runserver + 端口运行,默认8000
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 30, 2022 - 16:45:39
Django version 2.2.12, using settings siteA.settings
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
打开浏览器:http://127.0.0.1:8000/
如果出现错误消息“That port is already in use”(指定端口已被占用) , 请执行命令python manage.py runserver 8001 , 让Diango使用另一个端口
创建程序
当前,在前面打开的终端窗口中应该还运行着runserver 。 请再打开一个终端窗口(或标签页) , 并切换到manage.py所在的目录。 激活该虚拟环境, 再执行命令startapp :
C:\\Users\\Xiao>D:
D:\\>cd D:\\Pythonwork\\virtualenv
D:\\Pythonwork\\virtualenv>django2.2.12_learn\\Scripts\\activate
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>python manage.py startapp siteAPP
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>DIR
2022/11/30 16:44 131,072 db.sqlite3
2022/11/30 16:29 <DIR> django2.2.12_learn
2022/11/30 16:38 646 manage.py
2022/11/30 16:44 <DIR> siteA
2022/11/30 16:56 <DIR> siteAPP
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>dir siteAPP
2022/11/30 16:56 66 admin.py
2022/11/30 16:56 94 apps.py
2022/11/30 16:56 <DIR> migrations
2022/11/30 16:56 60 models.py
2022/11/30 16:56 63 tests.py
2022/11/30 16:56 66 views.py
2022/11/30 16:56 0 __init__.py
(django2.2.12_learn) D:\\Pythonwork\\virtualenv>
以上是关于windows python虚拟环境创建Django项目的主要内容,如果未能解决你的问题,请参考以下文章
windows同时创建python2和python3的虚拟环境