-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle relative imports in a package's __main__.py #2560
Comments
See subzero. |
We'd appreciate a pull-request implementing both entry-points and support for |
@JonathonReinhart What does the resulting |
@davidh-ssec I suspect you're not using relative imports in I put this example together, for reference: https://github.com/JonathonReinhart/pyinstaller-package-example When I build it (using # -*- mode: python -*-
block_cipher = None
a = Analysis(['foo/__main__.py'],
pathex=['/home/jreinhart/projects/pyinstall-package-example'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='foo_installed',
debug=False,
strip=False,
upx=True,
console=True ) And when I go to build + run it:
|
Did you ever solve this? I have the same problem. |
@dhowland The "workaround" as far as I'm concerned is to not use relative imports in your |
Note that's it's not necessary to use absolute imports everywhere. It's sufficient to use absolute imports in the script you're supplying to PyInstaller, only. |
…script with relative imports inside. The workaround is to use an intermediate stub file pyinstaller_entry.py. Reference to pyinstaller/pyinstaller#2560
So far, this is the workaround:
# __main__.py
from .component import *
...
def main():
...
if __name == '__name__':
main() # launcher.py
# Just copy __main__.py code to here and replace .component into component
from component import *
...
def main():
...
if __name == '__name__':
main()
|
hey, it would be cool if I could pass a setup.py to pyinstaller and it would figure it all out. Specifically also picking up the data directories. |
Freezing a |
Using only absolute imports in Project structure:
Contents of from logging.config import dictConfig
import coloredlogs # Needed for pyinstaller
import yaml
from mypackage.app import runApp # Absolute import
if __name__ == "__main__":
# Configure logging
with open("logging.yaml") as f:
dictConfig(yaml.safe_load(f))
# Run application
runApp() Function import sys
from PyQt5.QtWidgets import QApplication
from .components.main_window import MyWindow
def runApp():
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
sys.exit(app.exec()) |
I. e. remove relative imports from __main__.py. See: pyinstaller/pyinstaller#2560
Hi, just wanted to mention that I'm still encountering this bug in 2024. |
…orkaround pyinstaller/pyinstaller#2560, run pyinstaller REVHubInterface.spec to compile
How would resolving the full module path work in this scenario? i.e. If the hypothetical entry point script |
I'd assume the most obvious solution to be supporting the equivalent of |
We'd probably want to deprecate the current |
If we're ever to deal with the issue of relative imports in the entry point script (pyinstaller#2560) then we'll probably need to support Python's -m option and therefore remove the current interpretation of PyInstaller's -m option.
If we're ever to deal with the issue of relative imports in the entry point script (pyinstaller#2560) then we'll need to support Python's -m option and therefore remove the current interpretation of PyInstaller's -m option.
If we're ever to deal with the issue of relative imports in the entry point script (pyinstaller#2560) then we'll need to support Python's -m option and therefore remove the current interpretation of PyInstaller's -m option.
If we're ever to deal with the issue of relative imports in the entry point script (#2560) then we'll need to support Python's -m option and therefore remove the current interpretation of PyInstaller's -m option.
I think the best fix is just including documentation about how you can't have relative imports in the .py file you give to pyinstaller, but you can (maybe?) keep relative imports in all the other .py files. This resolved the issue for me. I think relative imports may get more common in the future. |
fix: Use absolute import in __main__ for pyinstaller/pyinstaller#2560 Closes IDFGH-14380 See merge request espressif/esp-idf-monitor!79
I have a runnable package with
__main__.py
:I can execute this package by running
python -m foo
. However you cannot directly create a PyInstaller application from this package:pyinstaller -F foo
, you'll getIOError: [Errno 21] Is a directory
.pyinstaller -F foo/__main__.py
it will build, but then you'll getValueError: Attempted relative import in non-package
when you try to run it, presumably because it took__main__.py
out of the context of its package.The work around is to create a small "stub" application that lives outside of the package and calls into it:
...and then point PyInstaller at the stub.
It seems that PyInstaller should be able to create an application by being pointed at the package directory.
I'm sorry if this has already been addressed, but I've been unable to find anything in the documentation or anywhere else:
The text was updated successfully, but these errors were encountered: