Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What parts of a virtualenv need to be changed to relocate it? [duplicate]

So, I've got a Python program with a ridiculous number of addons/packages. I'd like to be able to distribute the program in its virtualenv, so that the packages come bundled. However, the program is for Windows, and the "relocatable" feature of virtualenvs is unsupported on Windows (as well as still being experimental).

So, I'm looking at either writing a script, or just writing instructions to manually change absolute path names to relocate the virtualenv.

My question is if anybody knows where all I'd have to look for absolute path names in the virtualenv. I'm pretty new to Python packaging. The activate.bat script contains absolute path names, but do individual packages have absolute pathnames hard-coded into their installations?

The section Making Environments Relocatable describes why a virtualenv can't be simply moved, but doesn't list the places that contain absolute path names.

like image 899
jmite Avatar asked Jul 25 '11 17:07

jmite


3 Answers

Rather than trying to do this manually, you'd be better off using the freeze option to pip to create a requirements file. You can now rebuild your entire virtualenv with a single command.

On old virtualenv:

pip freeze > stable-req.txt

On the new one:

pip install -r stable-req.txt
like image 97
Daniel Roseman Avatar answered Sep 27 '22 22:09

Daniel Roseman


For your virtual environment directory {ENV} follow these steps

  1. Run $ virtualenv --relocatable {ENV}
  2. Edit {ENV}/bin/activate, using vim or editor of your choice
  3. Edit VIRTUAL_ENV (around line ~42!)to match the new directory location

So if you are looking to write a script, you can either export VIRTUAL_ENV shell variable or perform the edit on /bin/activate dynamically.

This is how I've setup mine.

# env/bin/activate
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# virtual env is at ./env
ENV_DIR=`dirname $BIN_DIR`
VIRTUAL_ENV=$ENV_DIR
export VIRTUAL_ENV
like image 23
Kalyan02 Avatar answered Sep 27 '22 21:09

Kalyan02


virtualenv-tools is a script to update an existing virtualenv's location after you move it. While it doesn't help on Windows, it might be useful to others.

It updates:

  • the virtualenv activation scripts
  • shebang lines in scripts in the virtualenv bin/ directory
  • absolute paths stored in .pyc files
  • symlinks in any local/ directory
like image 42
rcoup Avatar answered Sep 27 '22 22:09

rcoup