Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的模块有uu init uuuuuupy.py,Python仍然可以';我不能进口它_Python - Fatal编程技术网

我的模块有uu init uuuuuupy.py,Python仍然可以';我不能进口它

我的模块有uu init uuuuuupy.py,Python仍然可以';我不能进口它,python,Python,我有一个小python模块 +-- address_book | +-- models.py | +-- __init__.py +-- test | +-- test_models.py 当我运行python address\u book/test/test\u models.py时,我得到了这个错误 Traceback (most recent call last): File "address_book/test/test_models.py", li

我有一个小python模块

+-- address_book
|   +-- models.py
|   +-- __init__.py 
    +-- test
    |   +-- test_models.py 
当我运行
python address\u book/test/test\u models.py
时,我得到了这个错误

Traceback (most recent call last):
  File "address_book/test/test_models.py", line 5, in <module>
    from address_book import models
ImportError: No module named 'address_book'

您的文件结构应该如下所示

+-- address_book
|    +-- __init__.py
|    +-- models.py
     +-- test
     |    +-- test_models.py
     |    +-- __init__.py

要使代码正常工作,您还必须位于地址簿上方的目录中。

如评论中所述,您的树看起来很奇怪。将测试放在与地址簿相同的级别上

要运行测试,您应该使用自动测试发现工具,如nose或pytest(如有疑问,请使用pytest)。这些工具会自动找到它们正在测试的包,这样您就不必安装它们或以任何方式关心它们在路径中的存在

一开始它听起来像是一个负担,需要学习一个新的工具,但它非常简单,从长远来看,pytest确实很有帮助

There is problem with import of module from parent directory as __main__ lies in child folder
if you do the folder structure like below your same code for import will work

+----test
     +---test_models.py
     +---__init__.py
     +---address_book
         +---__init__.py
         +---models.py

或者,您可以将项目路径添加到PYTHONPATH变量中,同样的代码也可以工作

问题源于这样一个事实,即python不知道在哪里查找
地址簿
。可以通过在启动python之前设置
PYTHONPATH
环境变量来解决此问题

给定以下目录结构:

base_dir
+-- address_book
|   +-- __init__.py 
|   +-- models.py
    +-- test
    |   +-- test_models.py
那么这就行了:

$ env PYTHONPATH=/path/to/base_dir python address_book/test/test_models.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
PYTHONPATH
也可以是相对路径,只要相对路径指向当前工作目录中的源目录即可

为了避免每次键入
env PYTHONPATH=/path/To/base\u dir
,可以使用shell适当的语法进行设置。下面我使用bash语法设置PYTHONPATH环境变量

$ cd /path/to/base_dir
$ export PYTHONPATH=. # the current directory as a relative link
$ python address_book/test/test_models.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

作为旁注,我将改变您的目录结构,因为将测试保持在与源目录相同的目录树中不是一个好主意。通常,您可能会执行以下操作:

base_dir
+-- address_book
|   +-- __init__.py 
|   +-- models.py
+-- test # no __init__, not a module
|   +-- test_models.py

这使您可以更轻松地打包项目,而不必包含测试。

您的树很奇怪
\uuuu init\uuuuu.py
是一个文件,而不是一个目录,对吗?@S.deMelo我已经更新了树您从哪个目录运行测试?你可能会发现
python address\u book/test/test\u models.py
python test\u models.py
不起作用的地方起作用。@Dunes两者都不起作用实际上我用的是nose,它起作用了,但是如果我想生成测试代码覆盖率
覆盖率运行--source python address\u book/test/test\u models.py
它将不起作用,并给出上面相同的错误。使用nose时,我使用with the
--with coverage
标志。(我知道这并不能真正回答OP,但它可能会有所帮助。)
base_dir
+-- address_book
|   +-- __init__.py 
|   +-- models.py
+-- test # no __init__, not a module
|   +-- test_models.py