Samuel Samuel - 3 months ago 27
Bash Question

How to activate virtualenv in Cygwin

complete beginner here. Trying to build a flask web app. Using Windows 8.

Having some problems activating my python virtualenv in Cygwin. I have been using git shell up till now with no problems.

I copied my folder ("app") into my cygwin home directory and it is set up like so:

app - templates
- static
- flask - env - scripts - python
- ...
- hello.py
- ...


I change directory into the app folder, then when I type the command to activate my virtualenv:

$ source flask/env/scripts/activate


The terminal shows:

(env)


so I assume that it is working, until I double check which python:

$ which python


and that returns my original global python install, not the virtual environment. I've checked the installed packages to double check which python environment I am using.

I use the same command in git shell and it activates the right virtualenv. Where am I going wrong / what do I need to change? Please let me know if you need any more information.

I created a new virtual environment using cygwin and when I activated the new env, it switched to that environment fine. Why won't it work for the folder which I copied in?

Thanks,

Sam

Answer

You should not move the virtualenv. The activate script inside the virtualenv uses absolute paths internally. If you move the directory, the paths will no longer work, and so which python finds the first valid binary on PATH, which is your global binary.

If you need to move the project to a different location, and the virtualenv together with it, then recreate the virtualenv, do not copy it. The recommended practice is to have a requirements.txt file, and install packages using pip install -r requirements.txt. That way, recreating a virtualenv is very easy: create an empty virtualenv, and run the pip ... command. There should be nothing else inside the virtualenv that needs moving, only what pip put there, or other python installer scripts, if you used any (and which you would need to re-run, in addition to pip).