0

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

  • code/*.py
  • venv

And this is what I shipped to another machine.

kakyo
  • 10,460
  • 14
  • 76
  • 140
  • 2
    Virtualenvs are not portable! They're development tools but not distribution/deployment ones. – phd May 24 '19 at 16:20

2 Answers2

2

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

P.S
There is better alternative called Conda. You can provide the python version in conda envs.

andreygold
  • 192
  • 2
  • 13
1

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>"
kakyo
  • 10,460
  • 14
  • 76
  • 140