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

拈花ヽ惹草 提交于 2021-01-05 09:12:44

问题


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.


回答1:


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 workaround but am not 100% sure if it will work properly .

  • 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




回答2:


Maybe you can consider using pipenv to control the virtualenvs on different computer or environment.



来源:https://stackoverflow.com/questions/59460798/how-to-move-python-virtualenv-to-different-system-computer-and-use-packages-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!