Run python script using only the virtual environment folder

11,252

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
Share:
11,252

Related videos on Youtube

John Go-Soco
Author by

John Go-Soco

Updated on June 04, 2022

Comments

  • John Go-Soco
    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 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
    John Go-Soco over 5 years
    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).
  • ILoveYouDrZaius
    ILoveYouDrZaius over 5 years
    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.