shrewmouse
shrewmouse

Reputation: 6030

Can't seem to import modules using __init__.py

I am going to feel like a complete idiot when someone answers this question because I know that I have done this before.

Using Python 2.7.5

I have the following file structure:

sandbox/
|-- fun
|   |-- __init__.py
|   `-- spock.py
|-- __init__.py
`-- something
    |-- blah.py
    `-- __init__.py

Notice that there is an __init__.py file at each level. Each __init__.py is empty.

spock.py and blah.py are super simple.

spock.py

def helpmespock():
    print "help me spock!"

blah.py

import fun.spock

fun.spock.helpmespock()

Executing blah from sandbox and from sandbox/something results in the same error:

[me@computer sandbox]$ python something/blah.py 
Traceback (most recent call last):
  File "something/blah.py", line 1, in <module>
    import fun.spock
ImportError: No module named fun.spock

What am I doing wrong? Why can't I import spock.py from blah.py?


Solved

Thanks to everyone that responded. All of it was helpful.

Everything I did was right except for executing blah.py directly. I added test.py at the top-level which imports blah.py.

test.py:

import something.blah

something.blah.blah()

So now the tree looks like this:

sandbox/
|-- fun
|   |-- __init__.py
|   `-- spock.py
|-- something
|   |-- blah.py
|   `-- __init__.py
`-- test.py

Executing the test.py gives me:

[sri@s6000-devel sandbox]$ python test.py
help me spock!

Upvotes: 1

Views: 2435

Answers (5)

ExoWanderer
ExoWanderer

Reputation: 74

I cannot know if this is relevant to anyone else; but I encountered this problem because my system (Mac OSX 10.15.6) with Conda 4.8.3, the bashrc was not initializing correctly, and continously defaulted to the system python

# Open terminal:
python --version  # Python 3.7.7
which python  # $HOME/anaconda3/envs/env_name/bin/python
source $HOME/.bashrc
python --version  # Python 2.7.16
which python  # /usr/local/bin/python

As a result, my python interface was using the wrong version of python (2.7 instead of 3.7). In case that helps, check the version immediately before running the code; in my case the version was switched part way through developing.

Upvotes: 0

BadWolf
BadWolf

Reputation: 375

Problem is, you are directly running blah.py and import cannot resolve sandbox.fun.spock or fun.spock as parent, because __name__ is evaluated as "__main__".

So, __name__ should be "sandbox.something.blah" AND sandbox must be in path:

  1. Correct sandbox/something/blah.py to import correctly:

    from ..fun import spock
    
    spock.helpmespock()
    

    or even better

    from ..fun import spock
    
    def do():
        spock.helpmespock()
    
  2. Create a main to test blah (sandox/../test.py):

    import sandbox.something.blah
    
    sandbox.something.blah.do()
    

Upvotes: 1

pazitos10
pazitos10

Reputation: 1709

try adding ".." to your path:

import sys
sys.path.append("..")
from fun import spock

print spock.helpmespock() 

Upvotes: 1

Fabio Duran Verdugo
Fabio Duran Verdugo

Reputation: 172

You need add the sys.path

import os, sys                                                                  

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
import fun.spock                                                                

fun.spock.helpmespock() 

Upvotes: 1

matjazzz144
matjazzz144

Reputation: 76

try importing in the absolute sense

import sandbox.fun.spock

Upvotes: 0

Related Questions