[英]How can install Django project requirement.txt with python script
I am creating a single script for setup and running whole Django project. 我正在创建一个用于设置和运行整个Django项目的脚本。
venv_parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
venv_dir = os.path.abspath(os.path.join(venv_parent_dir, 'fvenv'))
subprocess.run(args=['virtualenv', '-p', 'python3', venv_dir])
os.popen('/bin/bash --rcfile %s'%(venv_dir+'/bin/activate'))
With the above code I created a virtual environment then activate this. 通过上面的代码我创建了一个虚拟环境然后激活它。 Now I want to install the
requirements.txt
file in the activated virtual environment 现在我想在激活的虚拟环境中安装
requirements.txt
文件
subprocess.run(args=['pip3', 'install', '-r', 'requirements.txt'])
I tried with subprocess
, but it's not installing in the virtual environment, it is installing in the operating system Python. 我尝试使用
subprocess
,但它不是在虚拟环境中安装,而是在操作系统Python中安装。
A the moment, the os.popen
command does not affect the environment that subprocess.run
runs in. That means that your subprocess.run
call is using the system pip3
instead of the pip
from the virtualenv. A中的时刻,
os.popen
命令不会影响环境subprocess.run
中运行。这意味着,你的subprocess.run
呼叫使用系统pip3
,而不是pip
从virtualenv中。 You can use the pip
from the virtualenv by using the full path: 您可以使用完整路径使用virtualenv中的
pip
:
import os
pip = os.path.join(venv_dir, 'bin', 'pip')
subprocess.run(args=[pip, 'install', '-r', 'requirements.txt'])
By using /path/to/venv/bin/pip
, you don't have to activate the virtual environment first. 通过使用
/path/to/venv/bin/pip
,您不必先激活虚拟环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.