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
Related
I have on my local machine following structure
all_code_root\
- utils\
-- some_specific_util.py
- project1\
-- app1.py
- project2\
-- app2.py
(utils and each project hosted in own git repository if that matters)
I'd like to set pycharm to work on a project1, while being able to recognize utils as a module, i.e. to be able to write in app1.py
from utils import some_specific_util
I've tried several options (marking the utils dir a source root, opening and attaching it as my project dependency etc), but I always end up only able to do this in app1
import some_specific_util # referring directly to the file works
#from utils import some_specific_util # this does NOT work, 'utils' is not recognized as a module name to import from
Is using utils.some_specific_util actually possible in pycharm? I see only workaround/hack to create artificial pycharm 'project' on all_code_root level, or create unneeded subdir for utils) (Also tried adding __ init __.py or using File - Invalidate caches from menu; my pycharm version is 2022.2.2)
If both python file and directory in same folder then this should be sufficient.
import utils
It will import all the Python files in the utils directory.
I have a Python library with Cython code.
When I compile the library I manage to import it successfully and use it in other projects. But when I write tests (which are executed from within the library's project root path), there's an import issue and the Cython code cannot be imported.
I have removed the project's path from sys.path so the import system will prefer to import from site-packages, and the import issue indeed occur in site-packages, but still, it happens only when I execute a file under the library's path, but does not happen when I import the library and execute a file under another project path.
Here's the project's structrue:
libname:
-libname/
call_mapping.pyx
main.py (imports call_mapping.pyx)
-tests/
test_libname.py (imports libname, raises ImportError because main.py can't import call_mapping.pyx)
But on another project, the error does not occur:
another-project:
f.py (import libname successfully)
The error:
from libname.main import LibnameSettings
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/libname-0.1.9-py3.10-macosx-10.9-universal2.egg/libname/main.py", line 6, in <module>
from libname.call_mapping import (
ModuleNotFoundError: No module named 'libname.call_mapping'
Any ideas on how to fix this? Thanks!
I am working on a project based on Python 2.7 and I am trying to import a module which is in a package folder that contains __init__.py and the file that I want to import called package1.py, but I am unable to do so. This is my folder structure: main_project/Tools/common/package1.py
Note that my project files are in the folder main_project. So, I am trying to call the package1.py by using an import statement in my script:
from Tools.common.package1 import variable
But I am getting an ImportError: No module named Tools.common.package1.
What is the solution to solving this error as I want to use the package feature for my project?
Maybe use the solution i found here :
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
or verify your module has a __init__.py
Importing files from different folder
Ok I have found the answer. I also had to insert an init.py in the folder Tools as well. Initially I only inserted init.py in common but not in Tools as we should also make Tools a package if we want to access common
I am working on some python project (2.7) and I have issue with imports. When I run main.py it start scripts from tests folder etc. and save output to logs and everything works fine.
/root------------
-logs
-staticCfg
-config.py
-tests
-systemTest
-scrypt1.py
-scrypt2.py
-userTest
-uScrypt1.py
main.py
My static variables (email, name etc.) are located in config.py. I need to import config.py in scrypt1.py or scrypt2.py. I tryed adding __init__.py to tests, systemTest and staticCfg folder but I always get an error.
In my scrypt1.py:
import staticCfg as cfg
...
or
from staticCfg import *
...
I get the error:
ImportError: No module named staticCfg
The import mechanism of Python can be a bit tricky.
You can refer to the documentation for more information: Python Import Mechanism
When you use absolute imports (your import does not start with a .) as you do, the import path will start from your main script (the one you launch). In your case, it's scrypt1.py. So starting from this location, python can't find the package staticCfg.
For me, the simplest solution is to create a main script in your root directory and call scrypt1.py from there (imported using from tests.systemTet import scrypt1.py). In this case, the base package will be your root folder and you will have access to the package staticCfg from all your script files as you wanted to do.
you may add root folder to PYTHONPATH.
I'm totally new to Python, and I want to create my first Python library for peronal uses.
I'm using Python 2.7.5, and running the IDLE interface.
So far, what I understood from the documentation and related questions is that:
Python goes through a list of directories listed in sys.path to find scripts and libraries
The package directory must contain a __init__.py file, which can be empty
The module I want to create should be a modulename.py file with the code inside the package directory
(Sources: http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html --- https://docs.python.org/2/tutorial/modules.html)
And here is what I tried that fails:
Created a personal package directory C:\....\pythonlibs
Created a subpackage dir C:\....\pythonlibs\package
Created the __init__.py file inside both folders
Created the mymodule.py file in the packacge dir
And then in the IDLE used this code:
import sys
sys.path.append(r'C:\....\pythonlibs')
First issue:
Currently I have to do this append every time I enter the IDLE. How can I keep the directory in sys.path permanently just as there are a lot of other directories there?
Then I tried importing my package:
import pythonlibs #fails!! why?
import pythonlibs.package #fails!! why?
import package #works
The error is: ImportError: No module named pythonlibs
Second issue?
This seems to be against the documentation, why can't I import from the root pythonlibs folder?
With line
sys.path.append(r'C:\....\pythonlibs')
you are instructing interpreter to start looking for modules (libraries) in this directory. Since this directory does not contain pythonlibs folder (the parent does), it can't import it.
Similarly - because it contains the module package, it can import it.