ImportError: No module named functions
ImportError: No module named functions
Image
我不知道我做错了什么。我正在从 tests
包中的 functions
包导入模块。我尝试了所有方法,但在尝试 运行 valid_test.py
时无法解决此问题
您需要在 函数 之前使用一个点,这样 python 就会知道它在文件夹 "above" 当前位置(import statement).
这看起来像这样 issue。
When specifying what module to import you do not have to specify the
absolute name of the module. When a module or package is contained
within another package it is possible to make a relative import within
the same top package without having to mention the package name. By
using leading dots in the specified module or package after from you
can specify how high to traverse up the current package hierarchy
without specifying exact names. One leading dot means the current
package where the module making the import exists. Two dots means up
one package level. Three dots is up two levels, etc. So if you execute
from . import mod from a module in the pkg package then you will end
up importing pkg.mod. If you execute from ..subpkg2 import mod from
within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification
for relative imports is contained within PEP 328.
还有另一种方法可以解决此问题,方法是将 "father" 文件夹添加到 sys.path
通过:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__, '..')))
Image
我不知道我做错了什么。我正在从 tests
包中的 functions
包导入模块。我尝试了所有方法,但在尝试 运行 valid_test.py
您需要在 函数 之前使用一个点,这样 python 就会知道它在文件夹 "above" 当前位置(import statement).
这看起来像这样 issue。
When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328.
还有另一种方法可以解决此问题,方法是将 "father" 文件夹添加到 sys.path 通过:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__, '..')))