Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upSystemError: Parent module '' not loaded, cannot perform relative import #94
Comments
This comment has been minimized.
This comment has been minimized.
|
You might try fiddling with Let me know if that doesn't help and I'll take a closer look. |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi I would also understand that we should not mess with sys.path, on the other hand relative imports exist for a reason, and in my opinion should be used, if they work.Regarding fiddling with |
This comment has been minimized.
This comment has been minimized.
It is not a goal of I'm happy to help you get |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi No problem, anyway I've never mentioned that I wanted |
This comment has been minimized.
This comment has been minimized.
|
@Dossan I don't know everything about idiomatic Python and there is unfortunately no canonical source. Nevertheless, relative imports are bad. Here are some other tips: https://docs.python.org/2/howto/doanddont.html |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi Honestly, apart from the fact that sometimes relative imports do not work in some environments, like with pdoc, I've still not understood why they are bad, even after reading what you suggested. For example, in my case, for example I have the following structure:
In I know this is another problem, and you don't have to answer. Thanks anyway! |
This comment has been minimized.
This comment has been minimized.
Not sure what else to add, then. Google around for "python relative import" and you'll see a whole bunch of issues. They make code harder to read and result in all sorts of weird import problems (as evidenced by googling). Python's import semantics are complicated enough as it is!
That's wrong. You should be able to do
The idiomatic way to build Python projects is to use a Unfortunately, |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi I would definitely stop using relative imports, but I am not able to make my scripts run without
As far as I understood, doing Since I have a lot of modules in a similar situation (complex but logical imports), this would be really tedious to do in all my submodules that need to import classes from other subpackages from parent packages. You said I have my project not well setup, do you know a guide where it explains how to deal with these complex situations of importing modules?
I knew already about virtualenv, but I am not seeing any advantage for my case, except from isolating the dependencies, which in my case are simply As far as I have understood, creating a People keep saying "avoid relative imports", and I would really avoid them, but at the end to make a simply module of 50-10 lines of code to run both as a script and as an imported module it takes other 10 lines of code with many hacks.
Believe me, I've gone through many tutorials and answers to questions on SO, but none of them tells me how to deal with complex imports that I need to do. It would be really nice to just be able to do absolute imports after specifying the main directory of your project. All imports would be absolute with respect to that main directory. This would be really a nice feature, which would never cause problems!!! |
This comment has been minimized.
This comment has been minimized.
It would make absolute imports work. If your Python package is installed, then it is accessible in a standard
The Python module system and its import semantics are complicated You're only hope for thriving in the ecosystem, including using other tools, is to follow ecosystem conventions. By operating outside of a I've linked to a few tutorials. I'd recommend starting there. There is no simple set of commands I can give you. You need to:
You're other choice is to ignore ecosystem conventions and do things your own way. I'm afraid that may make |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi Thank you very much! I really going to have a deeper look at Actually, I already know more or less how to work with a virtualenv, but not exactly with setting up the Actually, I really like this |
This comment has been minimized.
This comment has been minimized.
|
@Dossan No problem, good luck. I wish you the best! |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi I have set up a
from the root directory of my project, and it produces the following folders in the root folder of my project:
I am still not 100% sure of what's happening when I do
Whereas in my case I have just the following:
Would you know why? Apparently, even if I remove the files that are produced in the root directory after I know you are not obligated to answer, and thanks anyway! |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi Ahm, forgot to say, now the produced documentation with |
This comment has been minimized.
This comment has been minimized.
|
@Dossan We're really getting into the weeds here, and such details of Python's build system are far beyond my own comprehension. I could take guesses, and they may be right, but I'd rather not. I note that Python's ecosystem has been through several iterations and package formats over the years, and I have long since lost track of what's actually going on. I would recommend seeking out help through other channels, because I alone am quite a poor resource! I'm glad |
nbro commentedFeb 4, 2016
I am trying to create a html documentation for a module with the following command:
But I am receiving the following error:
I know that the problem is related to the fact that I am doing a relative import, which doesn't work because
huffman.pyis not being imported as I programmed it to be imported. So, currently, I am using the following code:Which is already a mess, because I am trying to handle both when this
huffman.pyfile (or module) is run as main or imported from outsideands, which is the parent of the parent of the package wherehuffman.pyis situated. To solve the problem, I did the following:Unfortunately this is far from being simple, considered that what I want to achieve should be very simple.
Any suggestions to simplify my code?