有没有办法升级在virtualenv中使用的python版本(例如,如果bug修复版本出来了)?
我可以pip freeze——local > requirements.txt,然后删除该目录和pip install -r requirements.txt,但这需要大量重新安装大型库,例如numpy,我经常使用它。
我可以看到这是一个优势,当从,例如,2.6 -> 2.7升级,但2.7呢?X -> 2.7.y?
有没有办法升级在virtualenv中使用的python版本(例如,如果bug修复版本出来了)?
我可以pip freeze——local > requirements.txt,然后删除该目录和pip install -r requirements.txt,但这需要大量重新安装大型库,例如numpy,我经常使用它。
我可以看到这是一个优势,当从,例如,2.6 -> 2.7升级,但2.7呢?X -> 2.7.y?
当前回答
I moved my home directory from one mac to another (Mountain Lion to Yosemite) and didn't realize about the broken virtualenv until I lost hold of the old laptop. I had the virtualenv point to Python 2.7 installed by brew and since Yosemite came with Python 2.7, I wanted to update my virtualenv to the system python. When I ran virtualenv on top of the existing directory, I was getting OSError: [Errno 17] File exists: '/Users/hdara/bin/python2.7/lib/python2.7/config' error. By trial and error, I worked around this issue by removing a few links and fixing up a few more manually. This is what I finally did (similar to what @Rockalite did, but simpler):
cd <virtualenv-root>
rm lib/python2.7/config
rm lib/python2.7/lib-dynload
rm include/python2.7
rm .Python
cd lib/python2.7
gfind . -type l -xtype l | while read f; do ln -s -f /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/${f#./} $f; done
在此之后,我可以在现有目录上运行virtualenv。
其他回答
我只是想澄清一下,因为有些答案指的是venv,有些指的是virtualenv。
在virtualenv上支持使用-p或——python标志,但在venv上不支持。如果你有多个Python版本,并且你想指定用哪个版本创建venv,在命令行上执行,如下所示:
malikarumi@Tetuoan2:~/Projects$ python3.6 -m venv {path to pre-existing dir you want venv in}
当然,正如其他人指出的那样,您可以使用venv进行升级,但前提是您已经升级了用于创建venv的Python。你不能升级到你系统上没有的Python版本,所以首先要确保获得你想要的版本,然后从它生成你想要的所有venv。
这个方法对我来说总是有效的:
# First of all, delete all broken links. Replace my_project_name` to your virtual env name
find ~/.virtualenvs/my_project_name/ -type l -delete
# Then create new links to the current Python version
virtualenv ~/.virtualenvs/my_project_name/
# It's it. Just repeat for each virtualenv located in ~/.virtualenvs
来自:
https://github.com/1st/python-on-osx#python-virtualenv https://gist.github.com/1st/ced02a1c64ac7b82bb27e432eea6b068
I moved my home directory from one mac to another (Mountain Lion to Yosemite) and didn't realize about the broken virtualenv until I lost hold of the old laptop. I had the virtualenv point to Python 2.7 installed by brew and since Yosemite came with Python 2.7, I wanted to update my virtualenv to the system python. When I ran virtualenv on top of the existing directory, I was getting OSError: [Errno 17] File exists: '/Users/hdara/bin/python2.7/lib/python2.7/config' error. By trial and error, I worked around this issue by removing a few links and fixing up a few more manually. This is what I finally did (similar to what @Rockalite did, but simpler):
cd <virtualenv-root>
rm lib/python2.7/config
rm lib/python2.7/lib-dynload
rm include/python2.7
rm .Python
cd lib/python2.7
gfind . -type l -xtype l | while read f; do ln -s -f /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/${f#./} $f; done
在此之后,我可以在现有目录上运行virtualenv。
给每个有问题的人
错误:命令'['/Users/me/Sites/site/venv3/bin/python3', '-Im', 'ensurepip', '——upgrade', '——default-pip']'返回非零退出状态1。
你必须安装python3.6-venv
sudo apt-get install python3.6-venv
你看到这个了吗?如果我没有误解这个答案,您可以尝试在旧的virtualenv之上创建一个新的virtualenv。你只需要知道哪个python会使用你的virtualenv(你需要看到你的virtualenv版本)。
如果您的virtualenv安装的python版本与旧版本相同,并且无法升级您的virtualenv包,您可能需要阅读这篇文章,以便使用您想要的python版本安装virtualenv。
EDIT
我已经测试过这种方法(在旧的virtualenv之上创建一个新的virtualenv),对我来说效果很好。我认为如果你把python 2.6改成2.7或者2.7改成3,你可能会遇到一些问题。X,但如果你只是在相同的版本中升级(保持在2.7),你应该不会有任何问题,因为所有的包都保存在两个python版本(2.7. X)的相同文件夹中。X和2.7。Y包在your_env/lib/python2.7/)中。
如果你改变了你的virtualenv python版本,你将需要重新安装该版本的所有包(或者只是将你需要的包链接到新版本的包文件夹中,即:your_env/lib/python_newversion/site-packages)