user3535074
user3535074

Reputation: 1338

Add relative paths to configuration file virtualenv_path_extensions.pth

I'm attempting to find a set of steps necessary to make a virtual environment of python 3.6 on windows relocatable.

1st I created a virtual environment on virtualenv 15.1.0 with the following command:

virtualenv
--always-copy
-a "path\to\project\dir"
-r "path\to\requirements.txt"
venv_name

After this, I run the following command to use the built in 'make paths relative ' functionality of virtualenv:

virtualenv --relocatable venv_name

Part of my requirements.txt is pypiwin32 library which, at least when installed via pip, wont work until the:

python Scripts/pywin32_postinstall.py -install

script is run (See here for details).

At this point, if I search the venv directory for clues of hardcoding, I see them in scripts\activate.bat, which I can make relative by changing this:

set "VIRTUAL_ENV=C:\path\to\venv"

into this:

pushd %~dp0..
set VIRTUAL_ENV=%CD%
popd

There are some other other places where I had to make slight adjustments to make them relative (I used the search in folder feature of sublime with my username as the search parameter - it brought up all the path\to\username\then\some\more style lines in the directory.

There are 2 hardcoded paths which are not so simple:

1. "path\to\venv\Lib\orig-prefix.txt"

I understand that orig-prefix.txt is a record of which is the source python installation on which the venv was based and so cannot really be relative but may need to be left blank if moving the venv to another machine (it's absence may crash the python launcher but its emptiness is fine.)

2. "path\to\venv\Lib\site-packages\virtualenv_path_extensions.pth"

This is trickier. As it is a hard-coded path which is then added to sys.path as a location to look for modules, when I move the venv to another machine where this path doesn't exist, the module load will fail.

Is there a way I can add relative paths to the configuration files such as virtualenv_path_extensions.pth

Upvotes: 2

Views: 6348

Answers (1)

amish
amish

Reputation: 355

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

This will make some of the files created by setuptools 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: scripts which have been made relocatable will only work if the virtualenv is activated, specifically the python executable from the virtualenv must be the first one on the system PATH. Also note that the activate scripts are not currently made relocatable by virtualenv --relocatable.

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.

Also, this does not make your packages cross-platform. You can move the directory around, but it can only be used on other similar computers. Some known environmental differences that can cause incompatibilities: a different version of Python, when one platform uses UCS2 for its internal unicode representation and another uses UCS4 (a compile-time option), obvious platform changes like Windows vs. Linux, or Intel vs. ARM, and if you have libraries that bind to C libraries on the system, if those C libraries are located somewhere different (either different versions, or a different filesystem layout).

Upvotes: 3

Related Questions