ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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
Last edited by Fergupicus; 11-04-2017 at 04:35 AM.
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.
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:
I'm not sure about the changes to bin/scriptcall.py - to me that looked like a recursive import (main imports scriptcall which imports main again). You may need to reorganize the code in that regard a bit.
With those changes, i could run it with python2 or python3, eg. like:
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?
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.
...says that from the first file (i/bin/scriptcall.py), the lines starting with "-" are removed, and are being replaced with the lines starting with "+" (from file "w/bin/scriptcall.py). In this case, the periods before "scriptcall" and "configuration" are being removed. Also, the line, "from .main import main" is being removed (notice that the new file has fewer lines in this section than the old file).
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. ;-)
@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!
Last edited by Fergupicus; 11-14-2017 at 02:09 AM.
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
/usr/bin/python: No module named bin.__main__; 'bin' is a package and cannot be directly executed
Python complains that there is no module named 'bin.__main__'.
I try to fill the 'bin.__init__.py' (see below)
Code:
% python -m bin
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib/python3.6/runpy.py", line 142, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/usr/lib/python3.6/runpy.py", line 109, in _get_module_details
__import__(pkg_name)
File "/home/jim/stage/X3cute-Framework/bin/__init__.py", line 1, in <module>
from bin import main
File "/home/jim/stage/X3cute-Framework/bin/main.py", line 2, in <module>
from .scriptcall import command
File "/home/jim/stage/X3cute-Framework/bin/scriptcall.py", line 2, in <module>
from .main import main
ImportError: cannot import name 'main'
Here a python stacktrace shows up: python's runpy.py appears on the top 3 places, and then it imports 'bin/__init__.py' and it fails in 'bin/scriptcall.py' with an ImportError. It can't import main from bin/scriptcall.py -- but we where already in bin/main.py
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
main.main()
With the following patch, you can start the X3cute-Framework via:
% python -m bin
Code:
% git diff
diff --git i/bin/__init__.py w/bin/__init__.py
index e69de29..206f820 100644
--- i/bin/__init__.py
+++ w/bin/__init__.py
@@ -0,0 +1,2 @@
+from bin import main
+main.main()
diff --git i/bin/scriptcall.py w/bin/scriptcall.py
index 8c7916b..cd0adf1 100644
--- i/bin/scriptcall.py
+++ w/bin/scriptcall.py
@@ -1,18 +1,14 @@
from .configuration import banner
-from .main import main
from .sub.help import help_page
def command(cmd):
if cmd == "banner":
banner()
- main()
elif cmd == "help":
help_page()
print(" ")
- main()
elif cmd.lower == "exit":
exit()
else:
print("Please select a proper option!")
- main()
\ No newline at end of file
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.