Run python script using only the virtual environment folder
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
Related videos on Youtube
John Go-Soco
Updated on June 04, 2022Comments
-
John Go-Soco almost 2 years
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 variablePIPENV_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.
-
phd over 5 yearsPossible duplicate of How to make a Python script standalone executable to run without ANY dependency?
-
John Go-Soco over 5 years@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 over 5 yearsYep, 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). -
ILoveYouDrZaius over 5 yearsI 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.