15

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.

guettli
  • 25,042
  • 81
  • 346
  • 663
jmite
  • 8,171
  • 6
  • 40
  • 81
  • Current doc at "Making Environments Relocatable" says (rightfully): _"Warning: The `--relocatable` option currently has a number of issues, and is not guaranteed to work in all circumstances. It is possible that the option will be deprecated in a future version of virtualenv."_ Recreating a python environment (e.g., via pip freeze/install requirements) is likely always going to be preferable to attempts to make one portable/relocatable. – michael May 28 '17 at 23:40

3 Answers3

15

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
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Will this work if I didn't install all of the programs with pip? Several of the addons (numpy, wxPython, PIL) couldn't be installed using PIP, and my searching seems to indicate they can't be installed with easy_install or PIP, though I could be wrong. – jmite Jul 25 '11 at 19:08
  • Looks like it will work for most of them, but it's not detecting wx, which had to be installed in the env with its windows installer, rather than by pip or easy_install. But freeze looks like a great tool, thanks! – jmite Jul 26 '11 at 15:49
  • Most of those packages which used to be troublesome for Windows (numpy, PIL, etc.) now have .whl files available. Using recent versions of pip, `pip install` should be able to find the .whl files for those packages, thus performing the installation as easily as any other package. – S. Kirby Apr 01 '16 at 00:43
6

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
Kalyan02
  • 1,416
  • 11
  • 16
3

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
rcoup
  • 5,372
  • 2
  • 32
  • 36