Skip to content Skip to sidebar Skip to footer

Relative Path Not Working Even With __init__.py

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/

Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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

Post a Comment for "Relative Path Not Working Even With __init__.py"