简体   繁体   中英

How to activate virtualenv in Cygwin after copying the virtualenv folder

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM