Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate virtualenv in Cygwin after copying the virtualenv folder

Tags:

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?

like image 611
Samuel Avatar asked Aug 29 '16 09:08

Samuel


People also ask

How do I activate virtualenv activation?

To activate virtualenv on Windows, first, install the pip. For this purpose, you can download and execute the latest Python installer. Next, install and create virtualenv on Windows using the pip package manager. Then, activate it using the “venvironment\Scripts\activate” command.

Which command is used to activate virtual environment after creating it?

Changed in version 3.5: The use of venv is now recommended for creating virtual environments.

How do I know if virtualenv is activated?

Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt. In the example above, the name of the environment is venv . If the name shows up, then you know that your virtual environment is active, and you can install your external dependencies.


1 Answers

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?

This last sentence is the real problem. The way you try to activate is correct. The problem is that the virtualenv directory must not be moved.

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).

like image 74
janos Avatar answered Oct 11 '22 16:10

janos