How to Move a Virtualenv

Can I move a virtualenv?

BUT ALAS:

No, you can't simply mv. There are workarounds, but it might be easier to reinstall.

(my-python-venv)$ /home/me/PeskyPartyPEnvs/pip3 install foaas
zsh: /home/me/PeskyPartyPEnvs/pip3: bad interpreter: /home/me/Env/my-python-venv/bin/python3: no such file or directory
(my-python-venv)$ deactivate
$

... presses enter a lot in frustration, and the following works

$
$
$ pip3 search foaas

Except it is not from my-python-venv, ergo sadness.

Want to mv your virtualenv and use it, otherwise unmodified?

Short Answer:

I'll let Boromir say it, so he can make it clear:

Well, ya can't.

Move the virtualenvs to another host folder

Virtualenvs are not by default relocatable. You can use virtualenv --relocatable <virtualenv> to turn an existing virtualenv into a relocatable one, and see if that works. But that option is experimental and not really recommended for use.

The most reliable way is to create new virtualenvs. Use pip freeze -l > requirements.txt in the old ones to get a list of installed packages, create the new virtualenv, and use pip install -r requirements.txt to install the packages in the new one.

Unable to clone Python venv to another PC

You can't copy-paste venvs from one machine to another since scripts in them may refer to system locations. (The same stands for attempting to move venvs within a machine.)

Instead, recreate the environment on the new machine:

  1. On the old machine, run pip freeze -l > packages.txt in the virtualenv.
  2. Move packages.txt over to the new machine.
  3. Create a new virtualenv on the new machine and enter it.
  4. Install the packages from the txt file: pip install -r packages.txt.

EDIT: If you don't have internet access on the second machine, you can continue from step 2 with:


  1. Run pip wheel -w wheels -r packages.txt in the venv on the first machine. This will download and build *.whl packages for all the packages you require. Note that this assumes both machines are similar in OS and architecture!
  2. Copy the wheel files over to the new machine.
  3. Create a new virtualenv on the new machine and enter it.
  4. Install the packages from wheels in the new virtualenv: pip install *.whl.

Renaming a virtualenv folder without breaking it

You need to adjust your install to use relative paths. virtualenv provides for this with the --relocatable option. From the docs:

Normally environments are tied to a
specific path. That means that you
cannot move an environment around or
copy it to another computer. You can
fix up an environment to make it
relocatable with the command:

$ virtualenv --relocatable ENV

NOTE: ENV is the name of the virtual environment and you must run this from outside the ENV directory.

This will make some of the files
created by setuptools or distribute
use relative paths, and will change
all the scripts to use
activate_this.py instead of using the
location of the Python interpreter to
select the environment.

Note: you must run this after you've
installed any packages into the
environment. If you make an
environment relocatable, then install
a new package, you must run virtualenv
--relocatable again.

Move a virtualenv to another PC with different python edition

I have already tried copying the virtualenv folder from PC1 to PC2 to
no avail.

What was the error you got? Did you activate the virtualenv on PC2? Is it on the same directory level as on PC2? For the activation, you can read up here:
https://virtualenv.pypa.io/en/latest/userguide/

good idea. One problem is that pip is not installed in PC2. Is it save
to copy pip?

PIP is also installed automatically in the venv, it should be located in ENV/bin/pip

How to move installed packages to a newly created virtual environment ?

While you could copy files/directories from the site-packages directory of your global installation into the site-packages of your virtual env, you may experience problems (missing files, binary mismatch, or others). Don't do this if you're new to python packaging mechanisms.

I would advise that you run pip freeze from your global installation to get a list of what you installed, and then store that output as a requirements.txt with your source, and put it under source management. Then run pip install -r requirements.txt after activating your virtualenv, and you'll replicate the dependencies (with the same versions) into your virtualenv.

Moving Python venv to another machine without internet

On you local machine (adapt the instructions if you are on Windows)

  1. Create your requirements.txt file
(venv) [...]$ mkdir pkgs
(venv) [...]$ cd pkgs
(venv) [...]$ pip freeze > requirements.txt
(venv) [...]$ pip download -r requirements.txt

  1. Download pip archive from here

  2. Copy pkgs folder to the remote machine

On the remote machine:


  1. Install pip from archive
(venv) [...]$ cd pkgs
# --- unarchive pip.tar.gz ---
(venv) [...]$ python setup.py install

  1. Install packages
(venv) [...]$ pip install --no-index --find-links . -r requirements.txt


Related Topics



Leave a reply



Submit