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 0
Add a comment  | 

2 Answers 2

Reset to default 0

In 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