在python中找不到模块
我有如下目录结构:在python中找不到模块,python,Python,我有如下目录结构: MainDir --FooBar --foobar.py, __init__.py | Ltests -- foobartest.py, __init__.py 在Foobertest.py内部, 我在进口 从FooBar.FooBar.func_导入FooBar* __init__.py in foobar contains from .foobar import * tests文件夹中的init.py是空的 但是当我试着经营食品店的
MainDir --FooBar --foobar.py, __init__.py
|
Ltests -- foobartest.py, __init__.py
在Foobertest.py内部,
我在进口
从FooBar.FooBar.func_导入FooBar*
__init__.py in foobar contains from .foobar import *
tests文件夹中的init.py是空的
但是当我试着经营食品店的时候
我得到这个错误
No module named FooBar.foobar.func_in_foobar
我认为你可以用索林在这里给出的答案来解决这个问题-:
您可以在MainDir中创建一个主文件,如
main.py
,该文件可以访问所有目录。对于测试,我发现成功地使我的测试依赖于我的python项目,并使用setuptools开发模式将其正确地放入path
import os, sys, inspect
# realpath() with make your script run, even if you symlink it :)
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# use this if you want to include modules from a subforder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# Info:
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
# __file__ fails if script is called in different ways on Windows
# __file__ fails if someone does os.chdir() before
# sys.argv[0] also fails because it doesn't not always contains the path