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:
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:
- On the old machine, run
pip freeze -l > packages.txt
in the virtualenv. - Move
packages.txt
over to the new machine. - Create a new virtualenv on the new machine and enter it.
- 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:
- 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! - Copy the wheel files over to the new machine.
- Create a new virtualenv on the new machine and enter it.
- 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)
- Create your
requirements.txt
file
(venv) [...]$ mkdir pkgs
(venv) [...]$ cd pkgs
(venv) [...]$ pip freeze > requirements.txt
(venv) [...]$ pip download -r requirements.txt
Download
pip
archive from hereCopy
pkgs
folder to the remote machine
On the remote machine:
- Install
pip
from archive
(venv) [...]$ cd pkgs
# --- unarchive pip.tar.gz ---
(venv) [...]$ python setup.py install
- Install packages
(venv) [...]$ pip install --no-index --find-links . -r requirements.txt
Related Topics
Removing Duplicates from Dictionary
Python Ctypes Issue on Different Oses
How to Move Pandas Data from Index to Column After Multiple Groupby
Heapq with Custom Compare Predicate
Matplotlib: How to Plot Images Instead of Points
In Python What Is a Global Statement
Fastest Way to Take a Screenshot with Python on Windows
Pil Thumbnail Is Rotating My Image
Executing Command Using Paramiko Exec_Command on Device Is Not Working
Typeerror: Str Does Not Support Buffer Interface
Python How to Write to a Binary File
Take Screenshot of Full Page with Selenium Python with Chromedriver
How to Get Around Declaring an Unused Variable in a for Loop
Django: Add Image in an Imagefield from Image Url