Can Existing Virtualenv Be Upgraded Gracefully

Can existing virtualenv be upgraded gracefully?

You can use the Python 2.6 virtualenv to "revirtual" the existing directory. You will have to reinstall all the modules you installed though. I often have a virtual directory for developing a module, and virtualenv the same directory with many versions of Python, and it works just fine. :)

Upgrade python in a virtualenv

Did you see this? If I haven't misunderstand that answer, you may try to create a new virtualenv on top of the old one. You just need to know which python is going to use your virtualenv (you will need to see your virtualenv version).

If your virtualenv is installed with the same python version of the old one and upgrading your virtualenv package is not an option, you may want to read this in order to install a virtualenv with the python version you want.

EDIT

I've tested this approach (the one that create a new virtualenv on top of the old one) and it worked fine for me. I think you may have some problems if you change from python 2.6 to 2.7 or 2.7 to 3.x but if you just upgrade inside the same version (staying at 2.7 as you want) you shouldn't have any problem, as all the packages are held in the same folders for both python versions (2.7.x and 2.7.y packages are inside your_env/lib/python2.7/).

If you change your virtualenv python version, you will need to install all your packages again for that version (or just link the packages you need into the new version packages folder, i.e: your_env/lib/python_newversion/site-packages)

How to change the python version of already existing virtualenv?

EDIT 1

Did some testing and found another more "graceful" way to (at least) update the executable. Let's assume the virtual env was initially created like so
virtualenv -p /path/to/my/python2.7 .venv. The executable can be updated to a specific python version like so: virtualenv --clear -p /path/to/my/python3.6 .venv. Please validate the python symlink in .venv/bin/python is updated using ls -la .venv/bin/python. The old executable(s) will still be in ./venv/bin/.

Note: You need to have the specific target version of python installed.


See this link which explains it well.

Virtualenvwrapper comes with some convenient commands for managing your virtualenvs.

To change your Python version:

  1. Deactivate your current environment session.

  2. If you have many packages or libraries installed, it would be a good
    idea to make a requirements.txt file. Remember to edit version as
    necessary.

  3. Remove the virtualenv with the wrapper command: rmvirtualenv

    • This will remove the virtualenv, but leave your project files.
  4. Make a new virtualenv with the Python version you want.

    • Example: mkvirtualenv -p python3 env-name

    • You can specify the Python version with the -p flag and version. If
      you have a requirements.txt file, you can specify that with -r
      requirements.txt

  5. Now bind your new virtualenv to your project directory. You can
    specify the full paths, but it is easier to have your new virtualenv
    activated and be in your project directory. Then, run the command:


Example: setvirtualenvproject

Please let me/us know if this answer was helpful to you!

How to upgrade virtual environment from previous Python version?

Use pkg_resources.find_on_path() to find packages using the same logic as pip freeze. This works even when the virtualenv is for an older version of 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 \
...

This is the trail of code that leads from pip freeze to find_on_path:

  • pip freeze accesses get_installed_distributions()
  • ... which accesses pkg_resources.working_set
  • ... which invokes working_set.add_entry() on each entry of sys.path
  • ... which invokes find_distributions() on each entry
  • ... which calls _find_adapter
  • ... which results in find_on_path

How do I update a Python virtual environment with `venv` (in Python 3.3+) to use a newer version of Python?

I guess what you're looking for is the --upgrade parameter.

python -m venv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip] [--prompt PROMPT]
ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
ENV_DIR A directory to create the environment in.

optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
--copies Try to use copies rather than symlinks, even when
symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it
already exists, before environment creation.
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual
environment (pip is bootstrapped by default)
--prompt PROMPT Provides an alternative prompt prefix for this
environment.

You need to run it with the targeted python version, for example in this case:

python3.8 -m venv --upgrade <path_to_dir>

Assuming that python3.8 is the name of your python 3.8.0 executable.

Update existing virtualenv to use Python 3.6

All binary packages installed for python3.5 (for example numpy or simplejson) are not compatible with python3.6 (they are not abi compatible). As such, you can't upgrade / downgrade a virtualenv to a different version of python.

Your best bet would be to create a new virtualenv based on the packages installed in the original virtualenv. You can get close by doing the following

edge/bin/pip freeze > reqs.txt
virtualenv edge2 -p python3.6
edge2/bin/pip install -r reqs.txt

Note that virtualenvs generally aren't movable, so if you want it to exist at edge you'll probably want the following procedure instead

edge/bin/pip freeze > reqs.txt
mv edge edge_old
virtualenv edge -p python3.6
edge/bin/pip install -r reqs.txt
# optionally: rm -rf edge_old


Related Topics



Leave a reply



Submit