How can I make PyServerer.spec files really portable? (woes the absolute path for the "pathex" parameter)

The PyInstaller .spec .spec means that it is ported on three supported platforms: Windows, Mac OS X, and Linux.

It is much easier for me to generate a .spec file once and modify it as I like before creating an executable file from it.

In every example on the Internet (like this one ), the .spec file .spec always determine the absolute path for the pathex parameter in the ANALYSIS section. This makes the assembly not portable, since the absolute path is not only specific to the machine on which the assembly is running, but also to the platform.

Should it always be an absolute path or is there a way to make it fully portable?

+7
source share
3 answers

If you put the .spec file in its default location, you can simply remove pathex from the specification because the 'current directory' and 'your-configured-pathex-here' identical.

explanation

pathex is an optional list of paths to search for sys.path

Source: https://github.com/pyinstaller/pyinstaller/blob/develop/PyInstaller/building/build_main.py#L123

The specification file is actually executable Python code. PyInstaller creates the application by executing the contents of the specification file.

Source: https://github.com/pyinstaller/pyinstaller/blob/develop/doc/spec-files.rst#using-spec-files

This leads to the conclusion that you should be able to hard-code each path for three environments, for example

 pathex=["/Users/fatuhoku/Python/myapp", "C:\\Python\\myapp", "/home/fatuhoku/Python/myapp"], 

or call a manual Python function that simply returns a list with a single element, which is the current working directory.

If you run PyInstaller, it will tell you something like this:

 46 INFO: Extending PYTHONPATH with paths ['current directory', 'your-configured-pathex-here'] 
+7
source

As others have already noted, a .spec file is Python code. You need pathex be the absolute path on the machine currently executing the code. You can do this with the global variable SPECPATH defined in the .spec file. (See All available global variables listed in the PyInstaller documentation here .)

In our case, I used:

 import os spec_root = os.path.abspath(SPECPATH) ... pathex=[spec_root] 

If your .spec not in the same directory as your package / script, you can use os.path.abspath(os.path.join(SPECPATH, '..')) or something similar to find your patex from your spec file.,

+3
source

Now pathex is converted to a list of absolute paths if it is not already one.

See PyInstaller build.py line # 426

The .spec file is certainly portable in the sense that it is literally the source code for Python. :) That way you can write any logic in your .spec file.

For example, my spec files reference helper routines that exclude most of the encoding. * data that does not bother me, _ssl, etc.

+2
source

Source: https://habr.com/ru/post/956570/


All Articles