admin管理员组文章数量:1393350
A directory has the following setup:
-root
-src
-tests
A module, duck_tester.py, is being run from within the tests directory. Both the src and tests directories (as well as the root repository directory) have an init.py file to allow importing from different folders.
duck_tester.py imports functions from the src directory:
from src.get_ducks_for_justice import get_ducks
from src.ducks import Noble_Duck
etc
These import correctly. However, the function get_ducks from within the module src.get_ducks_for_justice imports a module, config.py, from within the src folder. Within src.get_ducks_for_justice is the line
import config
Upon running duck_tester.py, an Error is returned:
Traceback (most recent call last):
File "C:\tests\duck_tester.py", line 24, in <module>
from src.get_ducks_for_justice import get_ducks
File "C:\src\get_ducks_for_justice.py", line 18, in <module>
import config
ModuleNotFoundError: No module named 'config'
The module config.py is in the src folder, where get_ducks_for_justice.py is stored. When running the get_ducks_for_justice.py module directly, the config.py module is imported correctly. However, the module is not found when the get_ducks_for_justice.py module is called from a module within the tests folder.
It is not possible to edit the import statement within *get_ducks_for_justice.py. Rather, a change must be made to duck_tester.py to allow this to run correctly.
What is the error in my approach?
A directory has the following setup:
-root
-src
-tests
A module, duck_tester.py, is being run from within the tests directory. Both the src and tests directories (as well as the root repository directory) have an init.py file to allow importing from different folders.
duck_tester.py imports functions from the src directory:
from src.get_ducks_for_justice import get_ducks
from src.ducks import Noble_Duck
etc
These import correctly. However, the function get_ducks from within the module src.get_ducks_for_justice imports a module, config.py, from within the src folder. Within src.get_ducks_for_justice is the line
import config
Upon running duck_tester.py, an Error is returned:
Traceback (most recent call last):
File "C:\tests\duck_tester.py", line 24, in <module>
from src.get_ducks_for_justice import get_ducks
File "C:\src\get_ducks_for_justice.py", line 18, in <module>
import config
ModuleNotFoundError: No module named 'config'
The module config.py is in the src folder, where get_ducks_for_justice.py is stored. When running the get_ducks_for_justice.py module directly, the config.py module is imported correctly. However, the module is not found when the get_ducks_for_justice.py module is called from a module within the tests folder.
It is not possible to edit the import statement within *get_ducks_for_justice.py. Rather, a change must be made to duck_tester.py to allow this to run correctly.
What is the error in my approach?
Share Improve this question asked Mar 12 at 7:52 acolls_badgeracolls_badger 4652 gold badges11 silver badges31 bronze badges 02 Answers
Reset to default 0In the code given the system path is set to '../tests' for the imported modules, as this is where duck_tester.py is being run from. When the imported modules themselves try to import, they are looking in '../tests' instead of '../src'. To address this, before importing modules within duck_tester.py, change the system path to '../src':
import sys
sys.path.append('../src')
In duck_tester.py
, try
from src import *
instead of
from src.get_ducks_for_justice import get_ducks
本文标签: python 3xModuleNotFoundError for function in another folder import from another folderStack Overflow
版权声明:本文标题:python 3.x - ModuleNotFoundError for function in another folder import from another folder - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744765393a2624007.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论