question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requires users to configure hidden imports when used in a PyInstaller project.

See original GitHub issue

appdirs does not suffer from this issue because all the source code is in one file appdirs.py

For example with app.py:

from platformdirs import user_data_dir

print(user_data_dir("appname"))

building this and running it gives an error due to the conditional imports PyInstaller can’t detect.

> pyinstaller app.py
> .\dist\app\app.exe
Traceback (most recent call last):
  File "app.py", line 1, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "platformdirs\__init__.py", line 31, in <module>
  File "platformdirs\__init__.py", line 27, in _set_platform_dir_class
  File "importlib\__init__.py", line 127, in import_module
ModuleNotFoundError: No module named 'platformdirs.windows'
[1348] Failed to execute script 'app' due to unhandled exception!

Users can fix this themselves by configuring a hidden import.

> pyinstaller app.py --hidden-import platformdirs.windows
> .\dist\app\app.exe
C:\Users\<User>\AppData\Local\appname\appname

However it would be nice if platformdirs provided a hook specifying the hidden imports. PyInstaller provides a mechanism for doing this https://pyinstaller.readthedocs.io/en/stable/hooks.html#provide-hooks-with-package Especially since I imagine the hidden import would be different depending on the platform.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jalazizcommented, Sep 2, 2021
1reaction
YariKartoshe4kacommented, Sep 2, 2021

I think it is a good suggestion, so it must be done. I wrote some TODO for discussion:

  • Create directory with hooks and registry it in setup.cfg
  • Add hook with hiddenimports, each platform has its own import Note: Pyinstaller does not support Android
  • Write tests to check changes ?Compile test binary and execute it in CI?

I suppose i can get started after adding user_documents_dir

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set up hidden imports in pyinstaller - Stack Overflow
The hiddenimports are used to specify imports that can't be detected by pyinstaller, not the paths to those imports.
Read more >
When Things Go Wrong — PyInstaller 5.7.0 documentation
If Analysis thinks it has found all the imports, but the app fails with an import error, the problem is a hidden import;...
Read more >
Pyinstaller: Module not found when running .exe when ...
I have tried explicitly importing openpyxl and that didn't work and I have also used --hidden-import : call venv\Scripts\activate pyinstaller --onefile ...
Read more >
Using PyInstaller to Easily Distribute Python Applications
You can also use --hidden-import multiple times in the same command. This option requires the name of the package that you want to...
Read more >
PyInstaller Documentation - Read the Docs
You can write “hook” files that inform PyInstaller of hidden imports. If you create a “hook” for a package that other users might...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found