1

Is that possible to build a self-contained Python virtual environment? By self-contained, I mean that all needed files to execute the program are there, including Python.

I'm using Poetry to manage venvs. Looking at venvs I created using poetry install I see that the dependencies are actually copied, but Python is simblinked.

For example:

>> ls -lah my-venv/bin/
python -> /Users/my-user/.pyenv/versions/3.11.2/bin/python

Also, I tried the virtualenvs.options.always-copy Poetry config, which translates to --always-copy virtualvenv configuration, but it didn't copy Python.

Since an expected answer would be "use containers", I say in advance that it's not an option for the given use case.

This question aims to a solution that an "all-in" directory/file can be uploaded to a Linux server and just run without using depending on any system-installed software.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
YFl
  • 845
  • 7
  • 22
  • The links to "external" resources are part of what makes it a *virtual* environment, rather than a "real" environment. – chepner May 07 '23 at 14:21
  • That's a really good comment on the semantics, actually. So may I rephrase. is there a way to package the real env all together? – YFl May 07 '23 at 14:42
  • Any tool to create [standalone scripts](https://stackoverflow.com/a/12059644/7976758), https://stackoverflow.com/search?q=%5Bpython%5D+standalone+script – phd May 07 '23 at 15:31
  • @ОлексійХолостенко Neither downvote nor upvote are mine so I cannot say. – phd May 07 '23 at 15:57
  • 1
    If you want something that does not need a Python interpreter to be preinstalled then: PyInstaller as already mentioned, [*briefcase*](https://pypi.org/project/briefcase/) also, or something like [*Nuitka*](https://pypi.org/project/Nuitka/), and see also [this section of documentation "*Bringing your own Python executable*"](https://packaging.python.org/en/latest/overview/#bringing-your-own-python-executable). -- Anyway, this question has been asked so many times, I am sure you can find lots of resources. – sinoroc May 07 '23 at 16:03
  • I thought of an ugly idea :) zip micromamba and the code, and run it through a script that unpacks, create conda env, and run in it ... funny idea – YFl May 07 '23 at 19:14

3 Answers3

2

If you want something that does not need a Python interpreter to be preinstalled then: PyInstaller as already mentioned, briefcase also might work, PyOxidizer, or something like Nuitka. I recommend you read this section of documentation "Bringing your own Python executable" as well.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
1

The simplest solution I found:

  1. Use conda/mamba to manage the dependencies. It has the advantage of actually installing Python, which is useful for the question use case.
  2. Use conda-pack to pack in a venv every Python user knows how to use.

Why this approach solves the problem? Because we end up with a venv including actual binaries, instead of symblinks. So we can't just share it as tar.gz file and no one (other than the project developers) has to learn new technologies (every average Python developer is likely to know venv). It just works:

# ON THE SOURCE/CI MACHINE
$ conda pack -n my_env -o my_env.tar.gz

# ON THE USER/SERVER MACHINE
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env
$ source my_env/bin/activate

A note: I tried using like PyInstaller and other tools people wrote in answers and comments. Honestly, they try to reinvent the wheel. For example, PyInstaller goes recursively over the imports and tries to build from the discovered imports. On my first try it didn't work for PyArrow *.pyx dependencies, for example. Maybe I used it wrong. But, why invent the wheel and discover the imports if we can go explicit with major tools like standard venv, Poetry, and Conda/Mamba.

YFl
  • 845
  • 7
  • 22
0

I think you could use a tool like PyInstaller. From documentation:

PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules.