7

In this question on S/O:

Can existing virtualenv be upgraded gracefully?

The accepted answer says that you can:

use the Python 2.6 virtualenv to "revirtual" the existing directory

I cannot seem to find any details on how to "revirtual" an existing virtualenv. I know how to manually install python, but I am looking very specifically for the "python / virtualenv" way to upgrade python inside a specific virtualenv.

In my very specific situation, I have become an administrator in someone else' stead. The system python is 2.6.6, however the virtualenvs are all using 2.7.4 within the virtualenv path, which is something like:

/home/user_name/.virtualenvs/application_name/bin/python2.7

While there is no python 2.7.x in the system. I cannot find any evidence of python having been manually installed, and I cannot find any details online about using pip or using apt-get/yum or anything to install different versions of python within the virtualenv.

So, my very specific questions are:

  1. How does one "revirtual" a virtualenv?
  2. How does one upgrade python within a virtualenv the "python" way?
  3. Are there alternative ways to manage python versions within virtualenvs?

Thanks, and please let me know if I can clarify my questions in any way!

-S

Community
  • 1
  • 1
stbonearth
  • 73
  • 5

1 Answers1

4

Usual caveat: make backup copies first. However, the structure created by virtualenv is not that complex, so it should be possible to either find what you need or create a new one and migrate. The path with ~/.virtualenvs in it means it was probably created with virtualenvwrapper so you could read up on that too.

virtualenv makes copies of the executables in the bin directory, which means they don't have to exist elsewhere. In the lib and includes directories, however, by default there will be symlinks to items from the "source" python (unless someone changed that setting). You could do a ls -l in those directories and maybe you will find where python 2.7 is installed - or maybe some broken symlinks.

Under lib should be one or more python-<version> directories, with some symlinks (probably) to python standard library stuff and a site-packages directory, which is where your installed packages are.

Now, upgrading. If you try to update the virtualenv "in-place" (i.e. you just run virtualenv --python==python<version> <existing-directory>) then you'll probably run into two issues: (1) The bin/python symlink will not be replaced unless you delete/move it, and (2) the directories under lib are per python version, so if you don't use 2.7 then the site-packages directory won't automatically carry over -- you'll have to reinstall packages, or go in and move it, which will probably work unless you have compiled binary extensions, or are migrating to 3.x, or something else weird happens.

An alternate, cleaner approach is to create a new, fresh virtualenv, and then reinstall the necessary packages. Find your own app's package(s) first and figure out if they have a setup.py that works or are just code that's sitting in a directory. If they don't have setup.py that correctly brings in all dependencies, then in your existing virtualenv folder you can run ./bin/pip freeze to get a listing of installed packages. You can save this to a file and use pip install -r <filename> on it later.

Jason S
  • 13,538
  • 2
  • 37
  • 42
  • Thanks Jason! I will try this later on this weekend! I can't upvote yet because I don't have at least 15 rep, but soon as I can I will. – stbonearth Aug 15 '14 at 19:51
  • So it definitely turns out that the previous admins were either using `virtualenv -p /path/to/python`, or were entering the virtualenv and (with virtualenv install within the virtualenv using `pip install virtualenv` from within the original virtualenv) creating the new virtualenv using `mkvirtualenv new_env_name` – stbonearth Aug 20 '14 at 20:39
  • -- Also very much agree with your upgrading comments, really probably not the best idea, and may or may not work depending on how the virtualenv was setup, etc. In my specific situation, I will be re-installing the OS soon on new servers to match versions better, so this is good! – stbonearth Aug 20 '14 at 20:47