3

I have this folder structure:

--test
    first.py
    --numpad
          second.py
          third.py

in the first.py i have this line of code:

from numpad import second

and in the second.py file i have this:

import third

but in the test folder when i run

python first.py

i get this error message:

ModuleNotFoundError: No module named 'third'

note: i have also tried adding __init__.py to my numpad folder but it didnt work

10
  • are you really trying to import second in second.py, or is that supposed to be import third? Commented Jun 5, 2019 at 8:42
  • @arne yes you are right,i edited ,thanks Commented Jun 5, 2019 at 8:44
  • Either way, the import probably doesn't work because the python interpreter doesn't know that numpad is supposed to be a python module. You can tell it that it is one by adding an empty __init__.py file into the numpad folder. Commented Jun 5, 2019 at 8:44
  • @arne i did that but didnt work, i added __init__.py in numpad folder but didnt work Commented Jun 5, 2019 at 8:45
  • 2
    @mhmighani right, I overlooked the python3.6 tag, the __init__.py stuff is python2. Next thing to try would be to do an explicitly local import for third, i.e. from . import third. Since your interpreter runs at a level where third is not visible, this might be necessary Commented Jun 5, 2019 at 8:50

2 Answers 2

2

Since the python interpreter is started within test, that's where it looks for imports. You can learn about pythons's search behavior for imports through the docs, if you're interested in the details.

To solve your problem, there are a bunch of ways to do it, the best one depending on how you plan to use your code. If you plan to write a library, it might make sense to package it, which would give you access to a global namespace that you can use.

But if you just want it to work right now, and only ever are going to run the interpreter from the same place (i.e. your test folder), defining the third file as a local one should do it:

second.py

from . import third

third.py

print('third here, not an import error')

This works for me:

~/test$ tree . 
.
├── first.py
└── numpad
    ├── second.py
    └── third.py
~/test$ python3.6 first.py
third here, not an import error
Sign up to request clarification or add additional context in comments.

Comments

0

I might be wrong on this, but I'm pretty sure you'd have to set up your environment variable to look in that specific folder. Which would be way more of a hassle than just adding your homemade modules to your default modules folder.

2 Comments

Current folder is usually a path where python looks for modules. You can verify this using print(sys.path)
Hmmm. I never actually knew that! For whatever reason, mine doesn't. Probably one of my coworkers screwing with the path variable. Pretty ironic lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.