简体   繁体   English

如何使用python脚本安装Django项目requirements.txt

[英]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.

相关问题 如何使用 anaconda 从 python 中的 Requirement.txt 安装包? - How to install packages from Requirement.txt in python using anaconda? 将Django App部署到Heroku,但是Heroku pip安装找不到与require.txt匹配的模块版本 - Deployong Django App to Heroku but Heroku pip install can't find module versions matching the requirement.txt python中的Requirement.txt文件 - Requirement.txt file in python 在ubuntu上pip install -r requirements.txt - pip install -r requirement.txt on ubuntu 如何使用空闲的 python 将requirements.txt 中提到的所有 python 包下载到 linux 的文件夹中? - how to download all the python packages mentioned in the requirement.txt to a folder in linux using python idle? 如何获得readthedocs.org构建以忽略我的require.txt? - How can I get readthedocs.org build to ignore my requirement.txt? 如何知道python模块正在使用哪个子模块? 无法为 docker 镜像创建requirement.txt - How to know which sub module are in use with python module?? unable to create requirement.txt for docker image pipenv 如何决定我的 python 版本以及为什么生成的requirement.txt 与原始版本不同? - How does pipenv decide my python version and why does the requirement.txt generated different from the original? 如何将required.txt中提到的所有python包下载到linux的文件夹中? - how to download all the python packages mentioned in the requirement.txt to a folder in linux? 未安装require.txt中的依赖项 - Dependency in requirement.txt not installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM