简体   繁体   中英

ModuleNotFoundError: No module named 'Src' - When I run a Pytest

I am building a discord bot and I am having trouble importing the code with the functions that I want to test.

When I run my MemberRepositoryTest.py file it gives me the following error:

ModuleNotFoundError: No module named 'Src'

MemberRepositoryTest.py (/Tests/Src/Repositories/)

import pytest

from Src.Repositories import MemberRepository # <--- This line is failing.

# TODO: Implement tests

My folder structure is this on this link - https://imgur.com/a/Rh65iJY

The tests structure is:

Tests/Src/Repositories/MemberRepositoryTest.py (Mirrors Src)

I am unsure why it is not finding my modules so I can import the classes and functions.

From my research online I think it may be a problem with my init .py files. However, being new to Python, I do not understand this fully. I read the Python documentation which explained it creates a package with the files inside as the modules. This means I have imported each file in the directory to the init .py file. However, I am still getting this issue.

Init file (on root directory):

import Src

Init file (/Src/):

import Cogs
import Entities
import Models
import Queries
import Repositories
import AppConfig
import DatabaseConnection

Init file (/Src/Repositories):

import GuildRepository
import MemberRepository

I run this from the root directory of the project (documents/rush_bot). The command I run to execute the test file is pytest Tests/Src/Repositories/MemberRepository.py

My Python Path Environment is:

C:\WINDOWS\SYSTEM32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages

Python is failing to find your package since it is not in the PYTHONPATH.

You can either add your project folder to the PYTHONPATH or . . If you add . then Python will always look for packages inside the current directory.

By the way, in this Init file (/Src/Repositories) you are importing modules without the full path.

import Src.Repositories.GuildRepository
import Src.Repositories.MemberRepository

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM