[英]Import error with unit tests: “No module named …” from local libary
I'm new to Python (JS developer) and am trying to run a testing suite. 我是Python(JS开发人员)的新手,正在尝试运行测试套件。 My project folder structure is as such:
我的项目文件夹结构如下:
project/
__init__.py
libs/
__init__.py
s3panda.py
tests
__init__.py
tests_s3panda.py
In terminal, I'm running python tests_s3panda.py
. 在终端中,我正在运行
python tests_s3panda.py
。
I don't understand how it's unable to find a local module: 我不明白如何找不到本地模块:
Traceback (most recent call last): File "tests_s3panda.py", line 7, in from libs.s3panda import S3Panda ImportError: No module named libs.s3panda
追溯(最近一次调用):文件“ tests_s3panda.py”,行7,来自libs.s3panda导入S3Panda ImportError:没有名为libs.s3panda的模块
tests_s3panda.py snippet: tests_s3panda.py片段:
from __future__ import absolute_import
import unittest
import pandas as pd
from libs.s3panda import S3Panda
class TestS3Panda(unittest.TestCase):
...
Doing from ..libs.s3panda import S3Panda
for relative path, I get: from ..libs.s3panda import S3Panda
作为相对路径,我得到:
ValueError: Attempted relative import in non-package
ValueError:尝试非包中的相对导入
I believe the fact that there is no init.py
in the top-level folder means that Python is not aware that libs
and tests
are both part of the same module called project
. 我相信顶层文件夹中没有
init.py
的事实意味着Python并不知道libs
和tests
都是同一个模块project
。
Try adding an __init__.py
to your project
folder, then write the import statement as from project.libs.s3panda import S3Panda
. 尝试将
__init__.py
添加到project
文件夹,然后from project.libs.s3panda import S3Panda
编写import语句。 In general, you want to specify absolute imports rather than relative imports ( When to use absolute imports ). 通常,您要指定绝对导入而不是相对导入( 何时使用绝对导入 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.