relative path not working even with __init__.py

筅森魡賤 提交于 2019-12-18 13:07:03

问题


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?


回答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




回答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.




回答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.




回答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.




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



来源:https://stackoverflow.com/questions/9427037/relative-path-not-working-even-with-init-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!