[英]Django deployment and virtual environment
I am aware that there are many questions regarding Django and virtual environments, but I cannot wrap my head around the use of virtual environments with respect to deploying my Django app (locally) via uwsgi/nginx.我知道有很多关于 Django 和虚拟环境的问题,但我无法围绕通过 uwsgi/nginx 部署我的 Django 应用程序(本地)使用虚拟环境。
My setup includes a virtual environment (with Django and uwsgi), my Django app, nginx and PostgreSQL. The app was created before the virtual environment, and I applied only a single change to manage.py
:我的设置包括一个虚拟环境(带有 Django 和 uwsgi)、我的 Django 应用程序、nginx 和 PostgreSQL。该应用程序是在虚拟环境之前创建的,我只对
manage.py
应用了一个更改:
#!/Users/snafu/virtualdjango/bin/python3
When I start up the uwsgi located in the virtual environment (with the appropriate .ini
file), everything works right away, but I wonder why.当我启动位于虚拟环境中的 uwsgi(使用适当的
.ini
文件)时,一切都会立即运行,但我想知道为什么。 I did not need to fiddle around with the $PYTHONPATH, or append the site packages directory to the system path in manage.py
, or activate the virtual environment at any point (apart from the initial installation of packages), although the boilerplate comment in manage.py
explicitly mentions an inactive virtual environment as a possible reason for an import error.我不需要摆弄 $PYTHONPATH,或 append 站点包目录到
manage.py
中的系统路径,或在任何时候激活虚拟环境(除了包的初始安装),尽管样板注释manage.py
明确提到不活动的虚拟环境是导入错误的可能原因。
Activating a virtual environment does nothing but prepend the virtual environment's bin/
to the $PATH
thus making python
and pip
without explicit paths running from the virtual environment.激活虚拟环境只会将虚拟环境的
bin/
添加到$PATH
前面,从而使python
和pip
没有从虚拟环境运行的显式路径。 Everything else related to virtual environments is implemented inside Python — it automatically changes sys.path
and other paths ( sys.prefix
, sys.exec_prefix
, etc).与虚拟环境相关的所有其他内容都在 Python 内部实现——它会自动更改
sys.path
和其他路径( sys.prefix
、 sys.exec_prefix
等)。
This means that when you run python
with an absolute path from a virtual environment Python automatically activates the virtual environment for this particular Python session. So you don't need to activate the virtual environment explicitly.这意味着当您使用虚拟环境中的绝对路径运行
python
时,Python 会自动激活此特定 Python session 的虚拟环境。因此您无需显式激活虚拟环境。
There is a minor warning sign on the road though: to run any Python script from a non-activated virtual environment you must set the shebang for all scripts to point to the virtual environment or use sys.executable
.不过路上有一个小警告标志:要从未激活的虚拟环境运行任何 Python 脚本,您必须将所有脚本的 shebang 设置为指向虚拟环境或使用
sys.executable
。 Do not use explicit python
because that could be a different Python from the $PATH
.不要使用明确的
python
,因为它可能是与$PATH
不同的 Python。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.