Folder structure and import problems with pytest
Imagine a simple folder structure:
my_folder/
__init__.py
funcs.py
tests/
test_funcs.py
funcs.py:
def f():
return 2
__init__.py:
from funcs import f
test_funcs.py:
from funcs import f
def test_f():
assert f() == 2
It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html
But when I run pytest from my_folder
:
tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'
It's strange because I would have thought that pytest
sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.
The documentation doesn't gave any indication about this either... They're just saying :
Typically you can run tests by pointing to test directories or modules:
pytest tests/test_appmodule.py # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir
What am I missing?
python python-import pytest
|
show 2 more comments
Imagine a simple folder structure:
my_folder/
__init__.py
funcs.py
tests/
test_funcs.py
funcs.py:
def f():
return 2
__init__.py:
from funcs import f
test_funcs.py:
from funcs import f
def test_f():
assert f() == 2
It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html
But when I run pytest from my_folder
:
tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'
It's strange because I would have thought that pytest
sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.
The documentation doesn't gave any indication about this either... They're just saying :
Typically you can run tests by pointing to test directories or modules:
pytest tests/test_appmodule.py # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir
What am I missing?
python python-import pytest
your__init__.py
you should havefrom .funcs import f
-- you are making a package-relative import
– Anthony Sottile
Oct 11 '18 at 20:36
@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error:E ModuleNotFoundError: No module named 'funcs'
– haxtar
Oct 11 '18 at 20:49
1
oh also your test file should havefrom my_folder.funcs import f
--$ python -m pytest -q my_folder
==>1 passed in 0.01 seconds
– Anthony Sottile
Oct 11 '18 at 21:07
That works! (when it is run outside of my_folder). But runningpytest
within my_folder still fails
– haxtar
Oct 11 '18 at 21:19
This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just sayspytest
...
– Yahya Abou Imran
Oct 11 '18 at 21:57
|
show 2 more comments
Imagine a simple folder structure:
my_folder/
__init__.py
funcs.py
tests/
test_funcs.py
funcs.py:
def f():
return 2
__init__.py:
from funcs import f
test_funcs.py:
from funcs import f
def test_f():
assert f() == 2
It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html
But when I run pytest from my_folder
:
tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'
It's strange because I would have thought that pytest
sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.
The documentation doesn't gave any indication about this either... They're just saying :
Typically you can run tests by pointing to test directories or modules:
pytest tests/test_appmodule.py # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir
What am I missing?
python python-import pytest
Imagine a simple folder structure:
my_folder/
__init__.py
funcs.py
tests/
test_funcs.py
funcs.py:
def f():
return 2
__init__.py:
from funcs import f
test_funcs.py:
from funcs import f
def test_f():
assert f() == 2
It's one of the suggested ways in the documentation:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html
But when I run pytest from my_folder
:
tests/test_funcs.py:1: in <module>
from funcs import f
E ModuleNotFoundError: No module named 'funcs'
It's strange because I would have thought that pytest
sets the path from where it's been running so those kinds of errors don't come up without dealing with it manually.
The documentation doesn't gave any indication about this either... They're just saying :
Typically you can run tests by pointing to test directories or modules:
pytest tests/test_appmodule.py # for external test dirs
pytest src/tests/test_appmodule.py # for inlined test dirs
pytest src # run tests in all below test directories
pytest # run all tests below current dir
What am I missing?
python python-import pytest
python python-import pytest
edited Oct 11 '18 at 0:49
Yahya Abou Imran
asked Oct 10 '18 at 19:50
Yahya Abou ImranYahya Abou Imran
849
849
your__init__.py
you should havefrom .funcs import f
-- you are making a package-relative import
– Anthony Sottile
Oct 11 '18 at 20:36
@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error:E ModuleNotFoundError: No module named 'funcs'
– haxtar
Oct 11 '18 at 20:49
1
oh also your test file should havefrom my_folder.funcs import f
--$ python -m pytest -q my_folder
==>1 passed in 0.01 seconds
– Anthony Sottile
Oct 11 '18 at 21:07
That works! (when it is run outside of my_folder). But runningpytest
within my_folder still fails
– haxtar
Oct 11 '18 at 21:19
This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just sayspytest
...
– Yahya Abou Imran
Oct 11 '18 at 21:57
|
show 2 more comments
your__init__.py
you should havefrom .funcs import f
-- you are making a package-relative import
– Anthony Sottile
Oct 11 '18 at 20:36
@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error:E ModuleNotFoundError: No module named 'funcs'
– haxtar
Oct 11 '18 at 20:49
1
oh also your test file should havefrom my_folder.funcs import f
--$ python -m pytest -q my_folder
==>1 passed in 0.01 seconds
– Anthony Sottile
Oct 11 '18 at 21:07
That works! (when it is run outside of my_folder). But runningpytest
within my_folder still fails
– haxtar
Oct 11 '18 at 21:19
This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just sayspytest
...
– Yahya Abou Imran
Oct 11 '18 at 21:57
your
__init__.py
you should have from .funcs import f
-- you are making a package-relative import– Anthony Sottile
Oct 11 '18 at 20:36
your
__init__.py
you should have from .funcs import f
-- you are making a package-relative import– Anthony Sottile
Oct 11 '18 at 20:36
@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error:
E ModuleNotFoundError: No module named 'funcs'
– haxtar
Oct 11 '18 at 20:49
@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error:
E ModuleNotFoundError: No module named 'funcs'
– haxtar
Oct 11 '18 at 20:49
1
1
oh also your test file should have
from my_folder.funcs import f
-- $ python -m pytest -q my_folder
==> 1 passed in 0.01 seconds
– Anthony Sottile
Oct 11 '18 at 21:07
oh also your test file should have
from my_folder.funcs import f
-- $ python -m pytest -q my_folder
==> 1 passed in 0.01 seconds
– Anthony Sottile
Oct 11 '18 at 21:07
That works! (when it is run outside of my_folder). But running
pytest
within my_folder still fails– haxtar
Oct 11 '18 at 21:19
That works! (when it is run outside of my_folder). But running
pytest
within my_folder still fails– haxtar
Oct 11 '18 at 21:19
This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says
pytest
...– Yahya Abou Imran
Oct 11 '18 at 21:57
This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says
pytest
...– Yahya Abou Imran
Oct 11 '18 at 21:57
|
show 2 more comments
2 Answers
2
active
oldest
votes
Here is a very simple way:
- Empty out
__init__.py
- From a level above
my_folder
runpython -m pytest
orpython -m pytest tests
(but notpytest
)
EXPLANATION: Running a module with the -m
option will include it in the PYTHONPATH
, so everything related to the import statements will be solved smoothly.
add a comment |
The test file test_funcs.py
must be run from the directory where the module funcs
is located in order for the import to be successful.
As a workaround, you can modify sys.path
, which determines the interpeter's search path for modules.
test_funcs.py
:
import sys
sys.path.append('/Users/Yahya/Desktop/my_folder')
from funcs import f
def test_f():
assert f() == 2
Just want to note: the path"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal,cd my_folder
, andpwd
.
– haxtar
Oct 10 '18 at 20:44
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
monkeypatchingsys.path
is almost never the right way
– Anthony Sottile
Oct 11 '18 at 20:34
The only other way to be able to usefuncs
intests/test_funcs.py
is to move__init__.py
(as is) frommy_folder
totests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"
– haxtar
Oct 11 '18 at 20:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52747763%2ffolder-structure-and-import-problems-with-pytest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a very simple way:
- Empty out
__init__.py
- From a level above
my_folder
runpython -m pytest
orpython -m pytest tests
(but notpytest
)
EXPLANATION: Running a module with the -m
option will include it in the PYTHONPATH
, so everything related to the import statements will be solved smoothly.
add a comment |
Here is a very simple way:
- Empty out
__init__.py
- From a level above
my_folder
runpython -m pytest
orpython -m pytest tests
(but notpytest
)
EXPLANATION: Running a module with the -m
option will include it in the PYTHONPATH
, so everything related to the import statements will be solved smoothly.
add a comment |
Here is a very simple way:
- Empty out
__init__.py
- From a level above
my_folder
runpython -m pytest
orpython -m pytest tests
(but notpytest
)
EXPLANATION: Running a module with the -m
option will include it in the PYTHONPATH
, so everything related to the import statements will be solved smoothly.
Here is a very simple way:
- Empty out
__init__.py
- From a level above
my_folder
runpython -m pytest
orpython -m pytest tests
(but notpytest
)
EXPLANATION: Running a module with the -m
option will include it in the PYTHONPATH
, so everything related to the import statements will be solved smoothly.
edited Nov 24 '18 at 10:21
answered Oct 11 '18 at 23:08
Yahya Abou ImranYahya Abou Imran
849
849
add a comment |
add a comment |
The test file test_funcs.py
must be run from the directory where the module funcs
is located in order for the import to be successful.
As a workaround, you can modify sys.path
, which determines the interpeter's search path for modules.
test_funcs.py
:
import sys
sys.path.append('/Users/Yahya/Desktop/my_folder')
from funcs import f
def test_f():
assert f() == 2
Just want to note: the path"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal,cd my_folder
, andpwd
.
– haxtar
Oct 10 '18 at 20:44
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
monkeypatchingsys.path
is almost never the right way
– Anthony Sottile
Oct 11 '18 at 20:34
The only other way to be able to usefuncs
intests/test_funcs.py
is to move__init__.py
(as is) frommy_folder
totests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"
– haxtar
Oct 11 '18 at 20:44
add a comment |
The test file test_funcs.py
must be run from the directory where the module funcs
is located in order for the import to be successful.
As a workaround, you can modify sys.path
, which determines the interpeter's search path for modules.
test_funcs.py
:
import sys
sys.path.append('/Users/Yahya/Desktop/my_folder')
from funcs import f
def test_f():
assert f() == 2
Just want to note: the path"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal,cd my_folder
, andpwd
.
– haxtar
Oct 10 '18 at 20:44
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
monkeypatchingsys.path
is almost never the right way
– Anthony Sottile
Oct 11 '18 at 20:34
The only other way to be able to usefuncs
intests/test_funcs.py
is to move__init__.py
(as is) frommy_folder
totests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"
– haxtar
Oct 11 '18 at 20:44
add a comment |
The test file test_funcs.py
must be run from the directory where the module funcs
is located in order for the import to be successful.
As a workaround, you can modify sys.path
, which determines the interpeter's search path for modules.
test_funcs.py
:
import sys
sys.path.append('/Users/Yahya/Desktop/my_folder')
from funcs import f
def test_f():
assert f() == 2
The test file test_funcs.py
must be run from the directory where the module funcs
is located in order for the import to be successful.
As a workaround, you can modify sys.path
, which determines the interpeter's search path for modules.
test_funcs.py
:
import sys
sys.path.append('/Users/Yahya/Desktop/my_folder')
from funcs import f
def test_f():
assert f() == 2
edited Oct 10 '18 at 20:42
answered Oct 10 '18 at 20:37
haxtarhaxtar
632827
632827
Just want to note: the path"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal,cd my_folder
, andpwd
.
– haxtar
Oct 10 '18 at 20:44
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
monkeypatchingsys.path
is almost never the right way
– Anthony Sottile
Oct 11 '18 at 20:34
The only other way to be able to usefuncs
intests/test_funcs.py
is to move__init__.py
(as is) frommy_folder
totests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"
– haxtar
Oct 11 '18 at 20:44
add a comment |
Just want to note: the path"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal,cd my_folder
, andpwd
.
– haxtar
Oct 10 '18 at 20:44
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
monkeypatchingsys.path
is almost never the right way
– Anthony Sottile
Oct 11 '18 at 20:34
The only other way to be able to usefuncs
intests/test_funcs.py
is to move__init__.py
(as is) frommy_folder
totests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"
– haxtar
Oct 11 '18 at 20:44
Just want to note: the path
"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder
, and pwd
.– haxtar
Oct 10 '18 at 20:44
Just want to note: the path
"/Users/Yahya/Desktop/my_folder"
is just an example. If you would like to determine the absolute path of my_folder, you can open a terminal, cd my_folder
, and pwd
.– haxtar
Oct 10 '18 at 20:44
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
I know about this workaround, but are you saying that the documentation is wrong, or at the very least obviously incomplete? (see the added quote)
– Yahya Abou Imran
Oct 11 '18 at 0:52
monkeypatching
sys.path
is almost never the right way– Anthony Sottile
Oct 11 '18 at 20:34
monkeypatching
sys.path
is almost never the right way– Anthony Sottile
Oct 11 '18 at 20:34
The only other way to be able to use
funcs
in tests/test_funcs.py
is to move __init__.py
(as is) from my_folder
to tests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"– haxtar
Oct 11 '18 at 20:44
The only other way to be able to use
funcs
in tests/test_funcs.py
is to move __init__.py
(as is) from my_folder
to tests
. Running pytest from my_folder after this transfer will result in a passing test. Unfortunately, though, the documentation specifically advises against it: "You shouldn’t have init.py files in your test directories"– haxtar
Oct 11 '18 at 20:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52747763%2ffolder-structure-and-import-problems-with-pytest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
your
__init__.py
you should havefrom .funcs import f
-- you are making a package-relative import– Anthony Sottile
Oct 11 '18 at 20:36
@AnthonySottile I considered that solution, as well. Unfortunately, running pytest with that modification still yields the same error:
E ModuleNotFoundError: No module named 'funcs'
– haxtar
Oct 11 '18 at 20:49
1
oh also your test file should have
from my_folder.funcs import f
--$ python -m pytest -q my_folder
==>1 passed in 0.01 seconds
– Anthony Sottile
Oct 11 '18 at 21:07
That works! (when it is run outside of my_folder). But running
pytest
within my_folder still fails– haxtar
Oct 11 '18 at 21:19
This proves that the documentation is missing something! You're even providing another way to run the tests! The doc just says
pytest
...– Yahya Abou Imran
Oct 11 '18 at 21:57