[PyQt] ImportError on pyqtdeploy project
Phil Thompson
phil at riverbankcomputing.com
Thu Apr 28 17:05:16 BST 2016
On 28 Apr 2016, at 12:40 pm, Marius Shekow <marius.shekow at fit.fraunhofer.de> wrote:
>
> Hi,
>
> I'm unable to start my Python 3.5.1 + Qt 5.5.1 Windows binary which I created using pyqtdeploy v1.2.1.
>
> My project/source structure is as follows:
>
> C:\codeprojects\myproject\sources\subproject1 < contains the source code of my project, also the entry point and all the Qt UI stuff. Uses also code from C:\codeprojects\myproject\sources\subproject2
> C:\codeprojects\myproject\sources\subproject2 < a different project (like a library)
>
> The C:\codeprojects\myproject\sources\ directory as well as all relevant sub-directories are packages, i.e. I did put a __init__.py file in each of these.
> When I work in the IDE (Pycharm), I set both the subproject1 and subproject2 as sources root (so PyCharm puts them into sys.path).
You should treat subproject2 as a separate package and add it under "Other Packages".
You can then scan the subproject1 directory rather than the sources directory.
You shouldn't need to set sys.path.
> I have done the following in pyqtdeploy:
>
> • Clicked on scan and selected C:\codeprojects\myproject\sources (because I need the code of both subproject1 and 2, so I selected the directory that is one level higher), clicked on "Include all" and removed the checkboxes of those sub-packages that I don't need to be distributed (e.g. Test-Suite code)
> • Entered this into "Entry point": sources.subproject1.main_gui:main
> • I left the "Main script file" form field empty
> • Entered this into sys.path (without quotes): ":/sources/subproject1 :/sources/subproject2" (space-delimited list)
> When freezing the application, this creates a build directory next to my pyqtdeploy project file, inside that build directory the resources directory has the sources dir (among others) which contains the frozen pyo files, as expected.
>
> When I try to start my exe file, I get the following output:
> Traceback (most recent call last):
> File "bootstrap.py", line 969, in _find_and_load
> File "bootstrap.py", line 958, in _find_and_load_unlocked
> File "bootstrap.py", line 664, in _load_unlocked
> File "bootstrap.py", line 634, in _load_backward_compatible
> File ":/sources/subproject1/main_gui.py", line 4, in <module>
> File "bootstrap.py", line 969, in _find_and_load
> File "bootstrap.py", line 944, in _find_and_load_unlocked
> File "bootstrap.py", line 222, in _call_with_frames_removed
> File "bootstrap.py", line 969, in _find_and_load
> File "bootstrap.py", line 956, in _find_and_load_unlocked
> ImportError: No module named 'gui_simple'
>
> This is what line 4 of subproject1/main_gui.py looks like:
> from gui_simple.mainwindow import MyMainWindow
> The corresponding directory and file is located at:
>
> C:\codeprojects\myproject\sources\subproject1\gui_simple\mainwindow.py
>
> So it should be correct that this line looks for a package or module named "gui_simple" (rather than "sources.subproject1.gui_simple..."), because it should really be found (due to setting sys.path).
> I also checked what sys.path looks like (by doing a import sys + print(str(sys.path)) ). This is the output, I don't know whether this is fine or not:
>
> [':/', ':\\sources\\subproject1', ':\\sources\\subproject2']
The backslashes are a bug - now fixed in hg. However if you get the structure right you don't need them anyway.
> Any ideas?
>
> The only way the import works is if I replace
> from gui_simple.mainwindow import MyMainWindow
> with
>
> from sources.subproject1.gui_simple.mainwindow import MyMainWindow
> But I really don't want to do that. This is what sys.path is for, after all!!
>
> Cheers!
> Marius
>
> Note: injecting help('modules') into my subproject1\main_gui.py (to be able to discover which modules are available) also failed with "NameError: name 'help' is not defined"
Phil
More information about the PyQt
mailing list