3

Let's stay I use pipenv to create a virtual environment. In setting it up, specify the python version in the Pipfile, and also have the environment variable PIPENV_VENV_IN_PROJECT set up so that the .venv folder is created in the project folder.

Inside the .venv folder, I find that it has all the packages I had specified in the pipfile, and also even the python executable of the version I specified.

If I copied my script and this .venv folder to another machine but which does not have Python installed, how do I go about running my script/s using just the .venv folder? There's a Python executable in there, but I'm trying to figure out how to get all the lib folders correctly as well.

Is this even possible? I know that alternative methods exist (such as pre-compiling the code using Cython/CXFreeze/etc.) but I was wondering about using just the virtual environment folder.

John Go-Soco
  • 886
  • 1
  • 9
  • 20
  • Possible duplicate of [How to make a Python script standalone executable to run without ANY dependency?](https://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency) – phd Oct 01 '18 at 18:17
  • @phd I would argue that it is not a _duplicate_- although the end goal is the same, I am asking if it is actually possible to do a distribution solely using the virtual environment (which I assume has everything). – John Go-Soco Oct 02 '18 at 07:16

1 Answers1

8

That is not the purpose of Python virtualenv. You have to regenerate the virtualenv when you move your script. The virtualenv can be different in every machine, depending of the OS, etc. For that exists the Requeriments.txt and that's why virtualenv's directory always appears in .gitignore files. However, once you have generated the virtualenv, you must use the python executable located in the virtualenv directory, as follows (assuming you are using unix):

venv/bin/python script.py

Or, using the activate script:

venv/bin/activate
python script.py
ILoveYouDrZaius
  • 239
  • 3
  • 8
  • Yep, I'm aware that this is the general workflow using a virtualenv. However, I was hoping someone could have some way to leverage the fact that pipenv does a pretty good job of `locking` the environment. I figure the `.venv` folder makes this locked environment portable too (among common machines, anyway). – John Go-Soco Oct 01 '18 at 08:48
  • I don't think so, because virtualenv does not itself provide the python interpreter. It allows you to create isolated environments in machines where a python interpreter is already available. – ILoveYouDrZaius Oct 01 '18 at 09:22