With python 3.10, I have a programm with this kind of structure:
.vscode
├── src
│ ├── moduleA
│ │ ├── __init__.py
│ │ └── mymodA.py
│ ├── moduleB
│ │ ├── __init__.py
│ │ └── mymodB.py
│ └── main.py
└── test.py
My targets :
Working progamm :)
Working pytest
Working coverage
I test multiples ways:
First
#main.py
from moduleA.mymodA import funcA
#mymodA.py
from moduleB import funcB
#test.py
from src.moduleA.mymodA import funcA
Results:
App : pass
pytest : failed with "Test result not found"
coverage : failed with "ModuleNotFoundError: No module named 'moduleB'"
Second
#main.py
from moduleA.mymodA import funcA
#mymodA.py
from src.moduleB import funcB
#test.py
from src.moduleA.mymodA import funcA
Results:
App : failed with "No module named 'src'"
pytest : pass
coverage : pass
I have add in the init.py for each module with no change:
#__init__.py
from .moduleA import funcA
I have test some other things but no many mor chance.
Did I have to use absolute path like ?
from importlib.machinery import SourceFileLoader
foo = SourceFileLoader("module.name", "/path/to/file.py").load_module()
foo.MyClass()
If you have any advise for me, thank you :)
Related
I'm trying to get started with python and pytest, I have the following project structure :
.
├── my_module
│ ├── __init__.py
│ ├── common
│ │ ├── __init__.py
│ │ └── utils.py
│ └── my_script.py
└── test
├── __init__.py
└── test_my_script.py
When I run tests (using pytest), I get an error:
no module named 'common'.
I have also the following all configs files:
tox.ini
setup.py
setup.cfg
pyproject.toml
someone know what I missed?
EDIT
here is how I import utils from test_my_script.py :
from common.util import func1,func2
common.util module is not accessible from your test execution because it is located in my_module package.
You should use import starting from working directory which is your project root.
Consider using following import instead:
from my_module.common.util import func1, func2
I have a project with the current structure, but some of my imports are not working when I think they should be. Shoudn't these imports work since the folders are properly marked as modules?
foo
├── app
│ ├── app.py
│ ├── folder1
│ │ ├── aaa.py
│ │ └── __init__.py
│ ├── folder2
│ │ ├── bbb.py
│ │ ├── __init__.py
│ ├── folder3
│ │ ├── ccc.py
│ │ ├── __init__.py
│ ├── __init__.py
│ └── main.py
├── README.md
└── .gitignore
WORKS
aaa.py
class X():
pass
main.py
from folder1.aaa import X
PWD: foo folder
CMD: python app/main.py
DOES NOT WORK
aaa.py
class X():
pass
main.py
from app.folder1.aaa import X
PWD: foo folder
CMD: python app/main.py
Traceback (most recent call last):
File "foo/app/main.py", line 1, in <module>
from app.folder1.aaa import X
ModuleNotFoundError: No module named 'app'
DOES NOT WORK
aaa.py
from app.folder2.bbb import Y
class X(Y):
pass
bbb.py
class Y():
pass
main.py
from folder1.aaa import X
PWD: foo folder
CMD: python app/main.py
File "foo/app/folder1/aaa.py", line 1, in <module>
from app.folder2.bbb import Y
ModuleNotFoundError: No module named 'app'
Python import works by searching the paths in sys.path.
check whether app is added to sys.path by running the below code
import sys
print(sys.path)
if it is not present in this list, append sys.path by including app directory.
import sys
import os
current_loc = os.path.realpath(__file__)
parent_dir = os.path.dirname(os.path.dirname(current_loc))
sys.path.append(parent_dir)
I want to import the module from another directory scripts/driver/scheduler.py to run.py and execute it, but every time I get the following error msg
ModuleNotFoundError: No module named 'scripts'
I added empty init.py files, but it didn't solve the issue
Here are a tree and the code:
.
├── __init__.py
├── pythonmodules
│ ├── module
│ │ └── run.py
│ └── setup
│ └── smthelse
└── scripts
├── driver
│ ├── __init__.py
│ └── scheduler.py
└── resources
└── smthelse
run.py
import argparse
import os.path as op
from scripts.driver.scheduler import scheduler
some lines of code
s = scheduler()
scheduler.py
import re
import sys
class scheduler():
some code
if __name__ == '__main__':
s = scheduler()
Could somebody explain why it doesn't work?
Somehow pythonpath was missing. The following export fixed it:
export PYTHONPATH=$PYTHONPATH:/home/user/myapp
When using pytest to run a specific test, I get the following error:
>>> pytest test/test_app.py
ImportError while importing test module '/home/connesy/code/test/test_app.py'.
Traceback:
test/test_app.py:3: in <module>
import __init__
E ModuleNotFoundError: No module named '__init__'
My code is structured like this:
├── src
│ ├── __init__.py
│ ├── conftest.py # Empty
│ ├── app.py
│ ├── server.py
├── test
│ ├── __init__.py
│ ├── test_app.py
│ ├── test_server.py
Each test has import __init__ at the top, and __init__ adds src to sys.path.
I need to be able to import and run tests from server.py, so test needs to be a package. How can I run a specific test with pytest with this structure?
I am building a relatively simple python module with 2 source python files.
The structure is like so:
├── MyModule
│ ├── MyModule
│ │ ├── __init__.py
│ │ ├── file1.py
│ │ ├── file2.py
│ ├── requirements.txt
│ ├── setup.py
inside of __init__.py I have
from .file1 import *
such that when imported I can simply type mymodule.myFunction()
and inside of file1.py I have
import file2
I then use pip install -e . to install the module.
However when I try to import it I get the following error:
----> 1 import file2
2 import matplotlib.pyplot as plt
3 import numpy as np
4 import scipy.signal
ImportError: No module named 'file2'
What is the accepted way I am supposed to go about doing this?
It seems like the problem was that when the file is imported into the init.py file it sees the current python environment where it is being imported and therefore cannot see file2.py. However if I instead inside of init type
from .file2 import *
from .file1 import *
and inside of file1 type
import MyModule
then I can use functions defined inside file2 inside file1 like so
MyModule.FunctionFromfile2(...)
Another way of doing this would be to initialise a sub-package as discussed here https://docs.python.org/2/distutils/examples.html .
I ended up using it as a subpackage as that was more sensible for my particular case the directory structure is now like so:
├── MyModule
│ ├── MyModule
│ │ ├── __init__.py
│ │ ├── file1.py
│ │ ├──MySubModule
│ │ │ ├── __init__.py
│ │ ├── file2.py
│ ├── requirements.txt
│ ├── setup.py
Inside of the __init__.py in MyModule I import file1 as from .file1 import * and inside of the __init__.py inside of MySubModule I import file2 as from .file2 import *.
Inside of file1 I then use MySubModule like so:
import MyModule.MySubModule
MyModule.MySubModule.FunctionFromfile2(...)
This has the benefit of separating the namespaces of the functions/objects from file1 and file2 when imported as a module. As the user sees MyModule.functionsFromfile1 and MyModule.MySubModule.functionsFromfile2.