0

I have some code that I can't get to import correctly [on Linux]:

Files (every python file contains just a class by the same name and capitalization):

commandreader/
|-- CommandReader.py
|-- y/
    |-- Switch.py
    |-- Option.py
    |-- __init__.py
    |-- x/
        |-- InputArg.py
        |-- __init__.py

CommandReader.py's import:

from y import Switch
from y import Option

y/Switch.py's and y/Option.py's import:

from x import InputArg

y/__init__.py:

from .import x
from .Switch import Switch
from .Option import Option

y/x/__init__.py:

from .InputArg import InputArg

Error:

$ python3 ./CommandReader.py
Traceback (most recent call last):
  File "CommandReader.py", line 12, in <module>
    from y import Switch
  File "/home/swatts/code/commandreader/y/__init__.py", line 2, in <module>
    from .Switch import Switch
  File "/home/swatts/code/commandreader/y/Switch.py", line 8, in <module>
    from x import InputArg
ModuleNotFoundError: No module named 'x'

Edit: Along with my error, do I misunderstand how Python wants packages to work? Because that's the impression I'm under.

  • What happens if you run `python3 -m CommandReader` instead? I would recommend using absolute imports first, and then eventually switch to relative once everything works well. See eventually this answer on a relatively similar question: https://stackoverflow.com/a/59768291/11138259 – sinoroc Jan 24 '20 at 11:21

1 Answers1

-1

One solution is, You can add the path of module in environmental variables:

Add the path in Path which is in Environmental Variable

If you are using Windows

  1. Right click on My Computer and go to Properties
  2. Select Advanced system settings
  3. Go to Tab Advanced
  4. Click on Environment Variables
  5. In System Variables Section Search for Path Variable
  6. Double click on it and add the path in the list of its value field.

You can also do using code:

import sys
sys.path.append(path)
print(sys.path)
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20
  • I am using Linux, and doesn't it seem like overkill to change an environment variable just to get this working, or do I just misunderstand how Python wants packages to be done? – Sawyer Watts Jan 24 '20 at 06:00
  • You need to specify the path of the module in the code if you don't want to alter the environment variables because python needs the references of that modules like from where the module is coming.Check this link. This might help you. https://docs.python.org/3/tutorial/modules.html – Vaibhav Jadhav Jan 24 '20 at 07:32
  • Gotcha, so Python doesn't support importing as I was trying to do in a manner similar to Java. – Sawyer Watts Jan 24 '20 at 16:54
  • I really doubt it is necessary to do these steps. As far I know, it is (almost) never necessary to modify `sys.path` or the environment variable`PYTHONPATH`. Instead in this case, it would probably be better to run code with `python -m CommandReader`, and change all imports to absolute imports. Then eventually once it's stable, progressively change back to relative imports if needed. – sinoroc Jan 27 '20 at 17:07