Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

python - Importing external module in single-file exe created with PyInstaller

I am using PyInstaller to create a single-file executable. Is it possible for my script to perform an import such that i) the imported module is imported from the same directory as the exe (i.e. it's not packaged into the exe) and ii) that imported module can import other modules that were packaged into the exe?

The background here is that the imported module contains configuration that the user should be able to modify. This may include creation of custom derived classes and use of enums from the packaged modules.

I haven't found any advice on this, though it's a difficult search because there are so many similar topics that use basically the same keywords.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following steps allow a Python module (named module_name here) outside of an executable created by PyInstaller to be imported and for that module to import modules that were bundled into the executable.

  • Add excludes=['module_name'] to the Analysis object used in the PyInstaller spec. This prevents module_name.py being bundled into the executable.
  • Add sys.path.append(os.path.dirname(sys.executable)) where module_name is imported in your application. This allows it to be imported from the directory the executable is in, which is different to the directory that the application will run in (due to being decompressed to a temporary folder). See below for my recommended method of achieving this.
  • Make sure any imports performed by the external module are also performed by one of the bundled modules before the external one is imported. The interpreter will not resolve the external module's imports against bundled modules, but will use ones that already exist in sys.modules.

In order to set up the paths correctly you can use the following:

if getattr(sys, 'frozen', False):
    app_path = os.path.dirname(sys.executable)
    sys.path.append(app_path)
else:
    app_path = os.path.dirname(os.path.abspath(__file__))

frozen is only available in generated executables, not when running as a script directly. This snippet will add the executable's location to sys.path if required as well as giving you easy access to the executable or script's location for use in code.

As an example of the final bullet point, consider the following.

# bundled_module1.py
import external_module
# bundled_module2.py
# module content
# external_module.py
import bundled_module2

This will fail in external_module.py because the imported module can't be found. However, the following will work:

# bundled_module1.py
import bundled_module2
import external_module
# bundled_module2.py
# module content
# external_module.py
import bundled_module2

This will be fine if there are a limited set of bundled modules that the external one should be able to import. It may get unwieldy for larger sets.

Given that the documentation states that the interpreter will resolve imports against modules bundled into the executable, this feels like a possible bug. Interoperating with modules outside of the executable isn't explicitly called out though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...