relative path not working even with __init__.py
Asked Answered
D

6

24

I know that there are plenty of similar questions on stack overflow. But the common answer doesn't seem to be working for me.

I have a file structure like this

  proj/
       lib/
          __init__.py
          aa.py
          bb.py
          test/
               __init__.py
               aa_test.py

I figured that if I include the code in my test.py

import lib.aa

or

from lib import aa

I would be able to reference the modules in the lib/ directory. But that did not work.

So I tried to add to path, and it adds it correctly:

os.environ["PATH"] += ":%s" % os.path.abspath(os.path.join("..",""))
print os.environ["PATH"]

but even now when I try the import statements above... I keep getting the error

ImportError: No module named aa

or

ImportError: Importing from non-package <Something...>

Is there something obvious I am missing?

Is there a way to check if I have configured my __init__.py files correctly, or to see my package hierarchy?

Daren answered 24/2, 2012 at 7:35 Comment(0)
O
28

You need to update your sys.path, which is where python looks for modules, as opposed to your system's path in the current environment, which is what os.environ["PATH"] is referring to.

Example:

import os, sys
sys.path.insert(0, os.path.abspath(".."))
import aa

After doing this, you can use your functions in aa like this: aa.myfunc()

There's some more information in the accepted answer for python: import a module from a directory

Oloughlin answered 24/2, 2012 at 7:49 Comment(1)
What about IDE's? They won't know about the modules being inserted dynamically like this. What a mess for python importsNerynesbit
Z
3

The lib directory needs to be in your python module search path, which isn't the same things as the search path used by your shell.

This will probably work for you:

import sys, os
sys.path.append(os.path.abspath(".."))

However, it is probably better to run your code from a context where the lib package is already on the path. Such as from the 'proj' directory.

Zeuxis answered 24/2, 2012 at 7:46 Comment(0)
D
2

I had similar problems and here is my advice.

Instead of changing sys.path, better run your test.py from being in proj (i.e. project root) directory. This way project dir will automatically be in sys.path and you will be able to import lib package.

And use absolute imports.

Davena answered 24/2, 2012 at 9:18 Comment(0)
F
1

Where is the code that you're trying to import lib.aa from? I'm guessing /proj/ is not your working directory and it would need to be as it's setup right now. Instead of PATH, you would want to add your directory to PYTHONPATH so it appears in the search path for an import. See http://docs.python.org/tutorial/modules.html#the-module-search-path

Also, please take a look at http://as.ynchrono.us/2007/12/filesystem-structure-of-python-project_21.html It strongly recommends you put an extra level of directory in place so instead of lib.aa, you would refer to it as my_proj.lib.aa.

Frendel answered 24/2, 2012 at 7:47 Comment(0)
G
0

System PATH variable is not used by python import statement. It uses PYTHONPATH, but best way to add new directory to import search path is to modify sys.path.

If this does not help, add to the question your value of sys.path and value returned by os.getcwd().

Grace answered 24/2, 2012 at 7:53 Comment(0)
U
0

In Sublime Text 3, abspath it didn't work for me. I use this instead in the top "__ init __.py" file

Hope it works for you.

from os.path import dirname
from sys import path

path.insert( 0 , dirname( __file__ ) ) ;

from test import aa_test
Unattended answered 26/7, 2021 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.