1

I'm having a hard time understanding this.

Lets assume I have a directory tree that looks like this:

~/speech
-- __init__.py
-- program.py
----------------
~/speech/parts
----__init__.py
----noun.py
----verb.py
----------------
~/speech/sentence
----__init__.py
----subject.py
----predicate.py

the __init__.py files are blank. I created them by issuing $ touch __init__.py
when i try to import anything, I get NameError: 'whatever' not defined. I've tried the whatever as both the directory name, and the individual file names.

every other problem i've had in python has been because I'm over-thinking things and trying to make it more complicated than it really is. ( curse the c++ habits! )

3
  • 1
    search SO for sys.path or PYTHONPATH...often discussed Commented Apr 5, 2011 at 3:58
  • Can you qualify what you mean by "import anything"? Commented Apr 5, 2011 at 4:22
  • When I read the tidbit from the python site that discusses the module search path, I get the feeling that it looks in ~/speech, ~/speech/parts, etc. When i say "import anything" i meant to convey that i can not import anything that's not in either /usr/local/lib/python or the dir where I issued python ./program.py When i'm creating a new program, where else should I be putting it other than in ~/someDirNameForPython? It doesn't make since (nor is it proper) to put it directly in the lib's while in devel Commented Apr 5, 2011 at 4:52

3 Answers 3

1

Unless ~ is your current directory or is in sys.path (which it shouldn't be) you won't be able to use any packages contained in it, including speech and its subpackages. Put the directory structure somewhere sane and add that path to $PYTHONPATH.

Sign up to request clarification or add additional context in comments.

Comments

1

you can quickly add your current directory to the python search path with:

export PYTHONPATH=$(pwd)

Here's some background information you should read:

Python's Module Search Path

Comments

1

Ok, i finally figured it out.

If I want it to look like "java-style classes", then I import via:

import parts.noun  
import sentence.subject  
parts.noun.defineNouns()  
sentence.subject.thePersonOrThing()  

If I want it to look more like C/++ style lib call, then I import via:

from parts.noun import defineNouns  
from sentence.subject import thePersonOrThing  
defineNouns()  
thePersonOrThing()  

*sigh* it's so simple, it's hard.

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.