I am trying to automate creating a project in Django with the following script.
我正在尝试使用以下脚本在Django中自动创建一个项目。
import os, sys, pip, virtualenv
directory = sys.argv[1]
if not os.path.exists(directory):
os.makedirs(directory)
new_dir = os.path.join(os.getcwd(), directory)
os.chdir(new_dir)
if not os.path.exists('venv'):
os.makedirs('venv')
venv_dir = os.path.join(os.getcwd(), "venv")
virtualenv.create_environment(venv_dir)
activate_script = os.path.join(venv_dir, "bin", "activate_this.py")
execfile(activate_script, dict(__file__=activate_script))
pip.main(["install", "--prefix", venv_dir, "pytz"])
The last line installs django
, but doesn't install pytz
. It says that pytz
is already installed, but when I activate the venv
, start python from within the venv
and import pytz
, it cannot load it. What am I doing wrong here?
最后一行安装django,但是不安装pytz。它说pytz已经安装,但是当我激活venv时,从venv中启动python并导入pytz,它就不能加载它。我在这里做错了什么?
1 个解决方案
#1
1
pip.main is not executing in the context of the virtual environment, but instead it tries to install pytz in your system distribution. That's why you get the message that it is already installed. You can use subprocess.call to specify which pip to use.
皮普。main不是在虚拟环境的上下文中执行,而是尝试在系统发行版中安装pytz。这就是为什么您会收到已经安装的消息。您可以使用子流程。调用以指定使用哪个pip。
import os, sys, virtualenv, subprocess
directory = sys.argv[1]
if not os.path.exists(directory):
os.makedirs(directory)
new_dir = os.path.join(os.getcwd(), directory)
os.chdir(new_dir)
if not os.path.exists('venv'):
os.makedirs('venv')
venv_dir = os.path.join(os.getcwd(), "venv")
virtualenv.create_environment(venv_dir)
subprocess.call(['{}/bin/pip'.format(venv_dir), 'install', 'pytz'])
#1
1
pip.main is not executing in the context of the virtual environment, but instead it tries to install pytz in your system distribution. That's why you get the message that it is already installed. You can use subprocess.call to specify which pip to use.
皮普。main不是在虚拟环境的上下文中执行,而是尝试在系统发行版中安装pytz。这就是为什么您会收到已经安装的消息。您可以使用子流程。调用以指定使用哪个pip。
import os, sys, virtualenv, subprocess
directory = sys.argv[1]
if not os.path.exists(directory):
os.makedirs(directory)
new_dir = os.path.join(os.getcwd(), directory)
os.chdir(new_dir)
if not os.path.exists('venv'):
os.makedirs('venv')
venv_dir = os.path.join(os.getcwd(), "venv")
virtualenv.create_environment(venv_dir)
subprocess.call(['{}/bin/pip'.format(venv_dir), 'install', 'pytz'])