1

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!
3
  • no, [sri@s6000-devel something]$ python blah.py File "blah.py", line 1 import ..fun.spock ^ SyntaxError: invalid syntax Commented Dec 4, 2015 at 14:41
  • Still no joy. (Python 2.7.5) [sri@s6000-devel something]$ python blah.py File "blah.py", line 1 from .. import fun.spock ^ SyntaxError: invalid syntax Commented Dec 4, 2015 at 14:47
  • Is the fact that there are no init.PYC files a clue? Commented Dec 4, 2015 at 14:59

5 Answers 5

1

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()
    
Sign up to request clarification or add additional context in comments.

1 Comment

For information, reference is available about intra-package references at docs.python.org/2/tutorial/…
1

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() 

3 Comments

The python path execution for something/blah.py is sandbox/something. blah.py doesn't know the path to fun, because it is a explicit execution. It would be different if you create a new file (test.py) with only import something.blah in the sandbox directory. Executing this file the path is sandbox (fun and something ar known) (sorry for my bad english)
you can know the path with sys.path
Your English is very good. I know that I've done this without manipulating sys.path before. You are correct, importing fun.spock from sandbox works.
1

try adding ".." to your path:

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

print spock.helpmespock() 

2 Comments

That works too but I shouldn't have to manipulate the search path directly. The init.py files are supposed to make the modules available. right?
You're right, maybe this will be useful to us: pythonic init.py
0

try importing in the absolute sense

import sandbox.fun.spock

4 Comments

Traceback (most recent call last): File "some/blah.py", line 1, in <module> import sandbox.fun.spock ImportError: No module named sandbox.fun.spock
Is your module inside the python path? Try positioning yourself into the directory that is parent to sandbox, then run the code.
That doesn't work either. {[sri@s6000-devel ~]$ python sandbox/something/blah.py Traceback (most recent call last): File "sandbox/something/blah.py", line 1, in <module> import sandbox.fun.spock ImportError: No module named sandbox.fun.spock }
Sounds like I'm not crazy then. What I'm doing should work.
0

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.

Comments

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.