Python Error: Parent module '' not loaded
Okay, so I haven't been on here in a while as I've been working on a python project. However I've run into a bit of an error with relative imports. So my project directory looks like this:
Code:
/X3cute The problem however is when I try to run main.py I get this error [I] SystemError: Parent module '' not loaded, cannot perform relative import. The code for the project is here. (Yes i know the __pycache__ is still in there. I was in a rush!) So any suggestions? Thanks in advance :) |
I'm just learning Python, myself, but I found a StackOverflow solution to this error that might be relevant:
https://stackoverflow.com/questions/...n-with-init-py Also, have you tried editing the "from .<name> import <method>" lines to reflect exact filenames and paths, including the .py extension? If that works, you could work backward to determine what it's choking on. |
Unfortunately I have already looked over these answers and none of them have helped. :( Also I can't seem to put in directories into the import statement as it returns an invalid syntax error.
Thanks anyway, your help is greatly appreciated :) |
Also not an expert here, eg. difference between python packages and python modules. I have no idea about those. But to me, it looks like that 'relative' path can only be used, if you're using python packages. For those you need to run the program like 'python -m <python_package_name>'
I've tried to run your project on my computer and had to do the following changes: Code:
With those changes, i could run it with python2 or python3, eg. like: Code:
python bin/main.py |
Sorry for radio silence!
Hey sorry for the radio silence, I had exams. :( I don't quite understand what you did there, could you please explain it to me? Haha sorry if it's obvious but it just looks random to me. For examples of what I don't understand:what are the +'s and -'s? what is index and diff? and what are the @@ lines for?
Thanks for your reply :) |
I can't speak to the code, itself, but I know a diff when I see one. :-D
The diff command specifies two files: for example, i/bin/scriptcall.py and w/bin/scriptcall.py. By convention, the old one is usually the first specified, and the new one is the last specified. So, you can think of it as "diff [options] old new". The "-" symbol in the output represents lines of the old file. The "+" symbol in the output represents lines of the new file. So, lines with "-" before them are being replaced with the lines that have "+" before them. Just to be sure, the lines starting with "---" and "+++" tell you which file is which, based on the directory path that was used when executing the command. @@ -1,6 +1,5 @@ Means that a change starts at line 1 of the old file (note the "-" symbol) and is 6 lines long. Its corresponding replacements start at line 1 of the new file ("+") and are 5 lines long. So, for example: Code:
--- i/bin/scriptcall.py A ways below that, you'll notice that the line, "main()" is being removed. (Note that I'm not giving a complete rundown of all changes made, just pointing out examples of how to read diff output.) It's possible to apply a diff to your code as a text file patch, using the "patch" command; but it's best to understand the code, before applying a patch. (You'll want to look up a tutorial on "patch" before you try it. It's useful for installing kernel patchsets, for example...) As to why these changes work...idunno...I'm new to Python. Hopefully, traene can tell you. ;-) |
Code:
man diff |
@DaneM (sorry if that's not how you tag someone, idk how this works :P ) Thanks for the speedy reply :) That was the answer I was looking for the diff thing!
|
Any time. :-)
(I don't know how tagging works on LQ, either.) |
Here is what i tried:
1. Clone X3cute-Framework from github % git clone https://github.com/Fergupicus/X3cute-Framework.git 2. Try to run it % python bin/main.py 3. Made some change and repeated 2 (until i got it somewhat running) The diff in my former post could be applied to your local copy of the X3cute-Framework, when you're on master branch. Eg. copy the lines to some temp file and try to apply those. I assume that you are also using git, if so then you could apply the patch with % git apply tempfile_name Alternatively, you could also use the patch utility directly, eg. apply the patch with: % patch -p 1 < tempfile_name You can start the X3cute-Framework then with: % python bin/main.py I'll try to give some more insight in another post. |
NOTE: That the above is probably not what you want, as you were already using the 'relative imports', eg. 'from .configuration import banner'. Those only work when you run your project as a python module. (This is also new to me, so sorry about my first attempt)
Let's try to start the programme with Code:
% python -m bin I try to fill the 'bin.__init__.py' (see below) Code:
% python -m bin Now, i've removed the calls to 'main' in bin/scriptcall.py -- as that seems to cause some 'recursive' import issue. Please see the changes to bin/__init__.py file. It now contains: Code:
from bin import main % python -m bin Code:
% git diff |
All times are GMT -5. The time now is 02:57 AM. |