Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pitfalls and workarounds when using Python virtual environments on Windows?

Short Description
The question is not meant to ask if using virtual environments are needed, but when using Ian Bicking's virtualenv what is the best way, if any, to manage environments in Windows. If you have multiple ideas, please answer multiple times so the best answers can be voted up. Thanks!

Background
When developing in python, I generally try use Mac osx / bash as much as possible. However there are always projects that I am forced to use Windows on for hardware/driver support. For these projects, I tend to rely on the 'double click' method to run the modules with the registered python.exe. The use of the 'double click' is such a simple way to run multiple threads / tests at the same time, without having to open a new command prompt, drill down to the directory needed, then typing 'python.exe module.py'.

The End Goal
I would like to have a way to manage and utilize virtual environments without having to be at the command prompt (Windows only)

Wish List
1. Be able to install modules from either pip (command line) or from binaries (for those that can't be installed with pip for whatever reason).
2. Manage environments, with virtualenv.exe, from a GUI. (Create, remove, list, activate, etc...)

Research
So far I have been able to do bits and pieces of my wish list using different scripts / methods, but have yet to find a way to combine them. I was planning to combine most of these into a GUI my self, but thought it would be wise to find out if there is a good reason this doesn't exist already.

Installing binaries to a virtual environment can be done fairly easy using a script that changes what version of python is registered in Windows. I have been using the script for several months now with nothing but great results. See SO Question

Managing the environments appears to the more difficult portion.

If using bash, there is virutalenvwrapper written by Doug Hellmann. I use this when working in Mac OSX and hightly recommend it.

There is a port of this exertion into Power Shell found here but still will require a third party command prompt interface to be installed.

One of the more unique ports to windows that I have seen ports virtualenvwrapper to bat files. I have not tested this, but it would still require a the use of the command prompt. Found here

The most promising helper function I have found to date is written by Justin Driscoll. While this exact example would require the command prompt, it would be trivial to convert this to something that a python GUI could call. This was the path I was going down before I thought I should check with the masses on the best way to achieve my goal.

like image 688
Adam Lewis Avatar asked May 04 '11 16:05

Adam Lewis


People also ask

What is the main reason why you should always use a virtual environment for your Python projects?

One of your projects might require a different version of an external library than another one. If you have only one place to install packages, then you can't work with two different versions of the same library. This is one of the most common reasons for the recommendation to use a Python virtual environment.

Should I use virtual environments Python?

Virtual Environment should be used whenever you work on any Python based project. It is generally good to have one new virtual environment for every Python based project you work on. So the dependencies of every project are isolated from the system and each other.


1 Answers

You might want to take a look at zc.buildout. Assuming the user has Python installed, you can double click on the bootstrap.py python script to generate the environment.

Once created, doubleclicking on bin\buildout.exe will recursively install dependencies and run any pre/post hook methods you define. zc.buildout allows you to specify platform specific dependencies and non-python dependencies. Additionally, you may define your own scripts for buildout to place into the bin\ folder. For example, the Plone team has a good article running Buildout on Windows referencing their own script bin\instance.exe

While not quite a polished as virtualenv on Mac/Linux/BSD, the same buildout environment will be created (eg. bin/buildout instead of bin\buildout.exe) achieving the cross-platform requirements you have.

like image 100
Erik Karulf Avatar answered Oct 17 '22 01:10

Erik Karulf