I trusted PyCharm to let it create a venv for me, and then shipped the code project with the venv folder. Inside my code, whenever calling a separate script, I use the venv Python uder venv/Scripts/
.
To my surprise, it breaks when running on another machine, where the venv python actually points to my local installation of Python at C:\\Python\\Python37
. This beats the purpose of creating a venv!
What have I done wrong? My project structure is like
And this is what I shipped to another machine.
Yes venv creates a link to your original python. The best practice to creating "venv requirements" is to create requirements file:
pip freeze > requirements.txt
Then just add this file to your project folder
PS
There is better alternative called Conda . You can provide the python version in conda envs.
Thanks to @AndreyG's answer. In addition, a bunch of others similar questions were helpful, like:
Copy complete virtualenv to another pc
Port Python virtualenv to another system
Especially after reading this tutorial: https://realpython.com/python-virtual-environments-a-primer/ ,
I realized my venv misconception, which was kinda due to my previous experience on shipping among Mac machines, where Python was usually preinstalled.
Now combined with the pip-freezing approach towards shipping site-packages
, I also ship the Python Windows installer and automate the installation to make sure that the venv symlinks point to the exact same Python installation on all machines. Reference:
https://docs.python.org/3/using/windows.html
The automation code:
python.<version>-<arch>.exe /quiet InstallAllUser=-1 PrependPath=1 TargetDir="C:\Python\Python<version>"
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.