1

I upgraded Python on my system from Python 3.5 to 3.6, and now all my virtualenvs created in Python 3.5 are no longer usable. How can I get a list of packages installed in a Python 3.5 virtualenv when I have only Python 3.6 installed? I need to setup a new Python 3.6 virtualenv with the same packages as in the old Python 3.5 virtualenv.

I know that I can look inside the lib/python3.5/site-packages directory and make the list manually, but I would prefer an automatic way of running e.g. pip freeze against the old virtual environment. I would prefer not to re-install the old version of Python.

I have tried the python -m venv --upgrade command, which has the help text "Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.". However, this doesn't actually reinstall the packages in the virtualenv, it just creates an essentially empty directory named lib/python3.6/site-packages. Furthermore, I had to remove the broken symlink bin/python3.5 in the venv in order to even run python -m venv --upgrade against the old virtualenv.

4

2 回答 2

1

用于使用pkg_resources.find_on_path()pip freeze. 即使 virtualenv 用于旧版本的 Python,这也有效:

rav@novascotia:~/venvs$ python
Python 3.6.1 (default, Mar 27 2017, 00:27:06) 
[GCC 6.3.1 20170306] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources
>>> entries = pkg_resources.find_on_path(None, './st-courses-3.5/lib/python3.5/site-packages')
>>> print('pip install \\\n%s' % ' \\\n'.join('%s==%s' % (o.project_name, o.version) for o in entries))
pip install \
websockets==3.2 \
webencodings==0.5 \
wcwidth==0.1.7 \
...

这是从pip freeze到的代码轨迹find_on_path

于 2017-05-22T18:57:08.943 回答
1

让 virtualenv 陷入无法升级的境地似乎很容易。您可能可以手动修复 venv,但我认为重新开始最容易。

简单来说:

  • 创建一个新的虚拟环境
  • 将旧 venv 中不属于 venv 环境的目录复制到其中。
  • 在旧环境中:ls -1 lib/python3.5/site-packages > requirements.txt,然后手动清理
  • 在新环境中pip install -r requirements.txt

请参阅此相关问题: 更新 Python virtualenv?

于 2017-01-09T23:07:57.023 回答