将全局包包含到使用 --no-site-packages 创建的 virtualenv 中
我通常更喜欢使用 --no-site-packages
选项创建 virtualenvs 以实现更多隔离,而且因为默认的 python 全局包包含相当多的包,并且通常大多数都不需要。 不过,我仍然想在全局中保留一些选定的包,例如 PIL
或 psycopg2
。有没有一种好方法可以将它们包含到 virtualenv 中,并且可以轻松实现自动化?
I'd usually prefer to create virtualenvs with --no-site-packages
option for more isolation, and also because default python global packages includes quite a lot of packages, and usually most of them are not needed.
However I'd still want to keep a few select packages in global, like PIL
or psycopg2
. Is there a good way to include them into the virtualenv
, that can also be automated easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 virtualenvwrapper 并且您也许可以使用 postmkvirtualenv 脚本自动在新的 virtualenv sitepackages 目录中创建符号链接。
If you're using virtualenvwrapper and you might be able to use the postmkvirtualenv script to automatically create symlinks in the new virtualenv sitepackages directory.
如果您使用 virtualenvwrapper,则 shell 命令
add2virtualenv
应出现在活动的 virtualenv 中。使用:向 virtualenv 站点包中的 PTH 文件
_virtualenv_path_extensions.pth
添加条目。使用 add2virtualenv 而不是自己创建符号链接的好处是,您可以通过注释掉 PTH 文件中的行来删除该包的可导入性。这使得您可以更轻松地根据代码所依赖的库的多个版本检查代码的有效性。
If you are using virtualenvwrapper, the shell command
add2virtualenv
should be present in an active virtualenv. Use:to add an entry to the PTH file
_virtualenv_path_extensions.pth
in your virtualenv site-packages.The benefit of using add2virtualenv rather than creating symlinks yourself, is that you can remove the package from being importable by commenting out its line in the PTH file. This makes it easier to check your code's validity against several versions of a library on which it depends.
我实际上还没有对这些特定的包进行过尝试,但我猜想从全局站点包到 virtualenv 的站点包的简单符号链接可能会起作用,并且这很容易编写脚本。
I haven't actually tried this with those specific packages, but I would guess that a simple symlink from the global site-packages into the virtualenv's site-packages might work, and this is easily scriptable.