mdeous
mdeous

Reputation: 18059

changing the python version used in a virtualenv

i've got a project in a virtualenv, which uses python2.6, but now i'd like to make it use python2.7. Is there a way to do this without having to backup my project files, re-create the virtualenv for the right python version, and then copy my files back in the virtualenv?

This does not seem to be a big task to do by hand, but being able to automatize this would still be very useful to easily test a project against many python versions, while still being in a virtualenv.

Upvotes: 5

Views: 4566

Answers (3)

KingRokHead
KingRokHead

Reputation: 41

This is 3 years late, but it may help someone else...

This response applies mainly to a Windows OS (7) as the primary development platform. These are the steps I would suggest to manage multiple Python virtual environments.

NOTE1: you may need to run your command prompt with Administrator privileges.

NOTE2: you may need to install a version of "setuptools" for each Python version; I'm not sure, as I haven't crossed that [proverbial] bridge yet. It shouldn't matter which easy_install is used to install virtualenv. The latest virtualenv should be retrieved and should support all versions of Python (my logical guess.)

NOTE3: Python .msi installers will write several entries to Windows' registry. So you may want to use only an archive distribution if you plan on developing and testing under several Python versions.

  1. Set up an environment variable (in addition to the required ones; i.e. PYTHONPATH) and call it PYTHONHOME. The PYTHONPATH variable should then reference the PYTHONHOME variable.
  2. Install each version of Python with which you need to test, which should just be a matter of simply unzipping the PythonX.X code-base to your development location of choice.
  3. To make switching between Python versions easy, simply create a .bat file for each version which sets the system environment variable (PYTHONHOME) for you. This is the approach I use.

    You could essentially use the following script command in a .bat file called "SetPython2.8.bat":

    setx /m PYTHONHOME "path\to\python\verion2.8"

  4. When you go to create your virtual [Python] environment, execute the specific .bat file according to the desired Python version, which for the example above, would be "SetPython2.8.bat".

    Right-click, run-as Administrator

  5. Create an additional .bat file for each Python virtual environment, simply for activation purposes. Now, when you need to install a particular library for a particular Python environment just execute (as Administrator) the particular .bat file, and install the libraries.

    Here are a set of commands for a hypothetical script called "Activate_Python2.8_VEnv.bat":

    title "VirtualEnv:Python2.8" set PATH_TO_WORKSPC="C:\path\to\workspace" cd %PATH_TO_WORKSPC%\VirtualEnvironments\python cd Python2.8 set "_OLD_VIRTUAL_PROMPT=$g" cmd /K Scripts\activate.bat Inspect the activate.bat file (in your virtual environment) to understand why we set "_OLD_VIRTUAL_PROMPT".

  6. Optionally, you can also choose to have a main system Python installation (as in 1). But, a virtual environment seems to be the way to go, as far as flexibility is concerned.

  7. [Feel free to improve]

Here is a thread with references on how to install different Python versions on a Mac or Unix platform

Upvotes: 4

jfs
jfs

Reputation: 414865

To easily test a project against many python versions you could use tox.

Upvotes: 3

Sam Dolan
Sam Dolan

Reputation: 32542

Just move your project outside of the virutalenv folders. They shouldn't be in there for this exact reason.

Using a different version of python may pull in slightly different packages, so it's best to just create a new virutalenv w/2.7 and install all your dependencies. Then when you want to test against different python versions just have your scripts activate and use the correct env.

Upvotes: 6

Related Questions