auto installing dependencies with pip to run a python zip application ?

Thomas Jollans tjol at tjol.eu
Tue Sep 26 15:19:59 EDT 2017


On 26/09/17 20:47, Irmen de Jong wrote:
> Hi,
> I've been using Python's executable zip application feature to neatly
> package my little game into a single "executable" file.
> Here "executable" means the user can simply start it by doubleclicking
> it, or launching it from a shell prompt. Of course the user already has
> to have a proper Python installation on their system but that's okay for me.
>
> However, I would like to also take care of the library dependencies of
> my game. Currently it requires pillow and sounddevice. If the user
> doesn't have these installed, the game exits with an error message
> telling the user to install these dependencies manually. I was thinking
> about a way to automate this so that you don't have to install anything
> manually to run the game.
>
> I was thinking about the following solution:
> - assume bare default Python (3.5+) installation
> - use venv.EnvBuilder() to create a new virtualenv somewhere in the
> user's home directory (~./virtualenvs/mygreatgame ?)

The appropriate place for this kind of thing, on Linux, would be
$XDG_DATA_HOME, default "~/.local/share/", i.e.:

f"{os.getenv('XDG_DATA_HOME', os.path.expanduser(
'~/.local/share'))}/{my_app}/{subdir}"

Other operating system have other conventions. (On Windows I think
os.getenv('APPDATA') is a good place to start)


> - use the with_pip=True argument so we also get pip installed into it
> - exec() the python executable from the new virtual env and let that
> "pip install -r mygamerequirements.txt"
> - inject "./mygame.pyz" in sys.modules[0]
> - run the game's main class and play!
>
> Notice that I'm not looking for a fully standalone executable created by
> things as cx_freeze, just something that is usable from within my zipped
> application.
>
> Any thoughts on this? Is it a good idea or something horrible? Has
> someone attempted something like this before perhaps?
>
> Irmen






More information about the Python-list mailing list