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
Related
I have a folder structure that looks like this:
├── repo
│ ├── src
│ └── main.py
│ ├── __init__.py
│ ├── utils
│ ├── __init__.py
│ └── helpers.py
│ └── predict
│ ├── __init__.py
│ └── predict.py
│
I am submitting a spark job with the --file src/predict/predict.py and a py-files src.zip (the spark submit command is built from main.py) of the entire source directory.
In predict, I want to reference a method in helpers.py. I do this by attempting an import: from src.utils.helpers import helper_func
However this does not work: ModuleNotFoundError: No module named 'src'. The imports work locally, but I am trying to understand how this works in the EMR. Ideally I dont want to have to change the importing from local to EMR. I just want them to be the same.
I have a problem where this is my project structure:
.
├── Resources/
├── src/
│ ├── __init__.py
│ ├── main.py
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── util.py
│ │ └── otherUtil.py
│ └── calculations/
│ ├── __init__.py
│ └── financials.py
└── tests/
├── __init__.py
└── test.py
My problem is that I can't reach the classes from the src/ folder from the tests, although the code in src/ can reach the Resources folder, through the first shown method.
I have tried:
To append the home library path this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Then this way:
Here I used the from src import util after these lines, I even tried from .src import util.
Than without the sys.path.append() with no use.
I have tried every combination I know, but for no use, and I don't want to install them as individual packages. Does someone have an idea, witch will solve my problem?
Clarification edit:
I don't want to put the tests in the source folder, i want to keep them separate.
You can use this code found here:
# test.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/utils/')
import utils
I'm trying to import a module from outside its directory in a.py.
The directory looks something like this:
.project
├── folder_1
│ └── a.py
|
├── folder_2
│ ├── __init__.py
│ └── b.py
My code in a.py
#contents of a.py
from ..folder_2 import b.py
But from this, I'm getting this error
ImportError: attempted relative import with no known parent package
I've been searching for a solution for this problem for quite a while but I haven't been able to find anything that helps.
With the following structure:
├── project
├── __init__.py
├── folder_1
│ ├── __init__.py
│ └── a.py
├── folder_2
│ ├── __init__.py
│ └── b.py
└── main.py
your a.py will not complain when executing main.py:
from project.folder_1 import a
Since project is a package for main.py and folder_1 and folder_2 are subpackages of it, you can use
Intra-package References.
If you want to directly execute a.py, you can simply do the following:
import sys
sys.path.append('..')
from folder_2 import b
I see the question has already been asked thousands times, but I'm utterly confused.
I'm trying to run hello.py which import utils.common into hello.py
from utils.common import function
And I get the following error:
ModuleNotFoundError: No module named 'utils'
So my structure is the following:
── gig
├── __init__.py
├── src
│ ├── __init__.py
│ ├── hello
│ │ ├── __init__.py
│ │ └── hello.py
│ └── utils
│ ├── __init__.py
│ ├── common.py
Provided __init__.py in gig and in src as well, nothing changes though.
P.S. Imports work fine in PyCharm, the issue arises in docker container or when I try to run it locally from terminal.
Any pointers are very much appreciated.
Cheers,
Giga
usually your full import path: gig.src.utils.common should work.
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?