通过 os.system() 激活 virtualenv
我正在编写一个基于 Python 的 shell 脚本,以使用 virtualenv、pip 和 Fabric 来制作 Django 应用程序的样板。应该足够简单,但似乎我无法通过 shell 脚本在 virtualenv 中激活和运行命令。
os.system('virtualenv %s --no-site-packages' % project_name)
os.system('source %s/bin/activate' % project_name)
os.system('easy_install pip')
运行时,出现错误:
$ startproject+ -s false sample
New python executable in sample/bin/python
Installing setuptools............done.
/testing
Searching for pip
Best match: pip 0.4
Processing pip-0.4-py2.6.egg
pip 0.4 is already the active version in easy-install.pth
Installing pip script to /usr/local/bin
error: /usr/local/bin/pip: Permission denied
显然 source
行没有运行,但为什么呢?这是并发/线程问题,还是 virtualenv 更深层的问题?
谢谢!
I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script.
os.system('virtualenv %s --no-site-packages' % project_name)
os.system('source %s/bin/activate' % project_name)
os.system('easy_install pip')
When running, this errors out:
$ startproject+ -s false sample
New python executable in sample/bin/python
Installing setuptools............done.
/testing
Searching for pip
Best match: pip 0.4
Processing pip-0.4-py2.6.egg
pip 0.4 is already the active version in easy-install.pth
Installing pip script to /usr/local/bin
error: /usr/local/bin/pip: Permission denied
Obviously the source
line isn't being run, but why? Is it a concurrency/threading issue, or something deeper with virtualenv?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次调用 os.system 都会在一个新的子 shell 中运行该命令,该子 shell 具有与原始 python 进程相同的属性。
尝试将命令放入一个以分号分隔的字符串中。
Each call to os.system runs the command in a new subshell, which has the same properties as the original python process.
Try putting the commands into one string separated by semicolons.
只是根本不要使用“源激活”。它除了改变 shell PATH 以将 virtualenv 的 bin 目录放在第一位之外什么也不做。我假设您的脚本知道它刚刚创建的 virtualenv 的目录;您所要做的就是通过完整路径调用 _virtualenv_dir_/bin/easy_install 。或者 _virtualenv_dir_/bin/python 用于在 virtualenv 中运行任何其他 python 脚本。
Just don't use "source activate" at all. It does nothing but alter your shell PATH to put the virtualenv's bin directory first. I presume your script knows the directory of the virtualenv it has just created; all you have to do is call _virtualenv_dir_/bin/easy_install by full path. Or _virtualenv_dir_/bin/python for running any other python script within the virtualenv.
每个 os.system 调用都会创建一个新进程。您需要确保
activate
和easy_install
在同一os.system
或subprocess
中运行称呼。Each
os.system
call creates a new process. You'll need to ensure that theactivate
and theeasy_install
are run in the sameos.system
orsubprocess
call.您还可以安装 virtualenvwrapper 并使用 postmkvirtualenv 挂钩。我使用它自动将 pip 和 IPython 的新副本引入我创建的 virtualenvs 中(因为我不希望它使用我的系统 IPython)。我还用它来将 pythonw 复制到 virtualenv 中,否则基于 wx 的东西将无法工作。看起来像这样:
You could also install virtualenvwrapper, and use the postmkvirtualenv hook. I use it to automatically bring in fresh copies of pip and IPython into virtualenvs I create (as I don't want it using my system IPython). I also use it to copy pythonw into the virtualenv, otherwise wx-based stuff won't work. Looks like this: