简体   繁体   中英

How to move Python virtualenv to different system (computer) and use packages present in Site-packages

I am making a python 3 application (flask based) and for that I created a virtualenv in my development system, installed all packages via pip and my app worked fine.

But when I moved that virtualenv to a different system (python3 installed) and ran my application with the absolute path of my virtualenv python (c:/......./myenv/Scripts/python.exe main.py) then it threw the errors that packages are not installed, I activated the virtualenv and used pip freeze and there were no packages were installed.

But under virtualenv there is 'Site-Packages' (myenv -> lib -> site-packages) , all my installed packages were persent there.

My Question is how to use the packages that are inside 'site-packages' even after moving the virtualenv to different system in Python 3.

Moving a virtualenv from a computer to another, and even on the same computer from a location to another is a bad idea , and this is why :

  • Since a lot of the binaries and libs are symlinks, and linked to your old system binaries and libs, it won't work on other machines.
  • Since many of bin/ scripts in your virtualenv depends on the virtualenv path on the system , it won't work if you moved the virtualenv to another location (even on same system either .)

So the recommend way is :

  • First generate requirements.txt file :

     pip freeze > requirements.txt
  • Second after moving everything (except the virtualenv directory) create a new virtualenv, activate it and run :

     pip install -r requirements.txt

Finally in your case if you really didn't generated a requirements.txt file, and need to use the old site-packages , there is a dirty workaround which i tried once on a gnu/linux machine and somehow worked but am not 100% sure if it will work properly so if you want give it a try.

  • copy the site-packages in your-old-virtualenv/lib/python{version}/ somewhere in your new computer , Desktop for example
  • Delete the old virtualenv, and create a new virtualenv
  • Replace the site-packages in the new virtualenv in new-virtualenv/lib/python{version} with the old site-packages
  • Delete __pycache__ folder in the newly copied site-packages
  • Activate the new virtualenv and test if everything is working .

Note that you should use the same python version either 2 or 3 , don't expect a virtualenv that depends on python2 to run properly with python3

也许您可以考虑使用pipenv来控制不同计算机或环境上的 virtualenv。

You Must not copy & paste venv, even in the same system.
If you install new package in venv-copied , then it would installed in venv-original .
Becaus settings are bound to specific directory.

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