Python package not being found on Pytest - python

I have the following project structure:
foo
│
├── foo
│ ├── cli
│ │ ├── tests
│ │ └── __init__.py
│ ├── core
│ │ ├── tests
│ │ └── __init__.py
│ ├── tests
│ │ ├── __init__.py
│ │ └── test_foo.py
│ ├── __init__.py
│ └── foo.py
├── kube
├── requirements
├── ...any-other-non-source-related
└── README.md
Inside the foo/foo.py file I have the following placeholder code:
import cli
import core
def say_hi():
print('hi')
if __name__ == '__main__':
say_hi()
And on the file foo/tests/test_foo.py I have the following:
from foo import foo
def test_foo():
foo.say_hi()
assert False
Then, in shell, I'm getting the following:
$ python foo/foo.py
hi
$ pipenv run pytest
===================== test session starts =====================
platform linux -- Python 3.7.4, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: /xxx/xxx/xxx/xxx/python-project-folder-structures
collected 0 items / 1 error
===================== ERRORS =====================
_____ ERROR collecting foo/tests/test_foo.py _____
ImportError while importing test module '/xxx/xxx/xxx/xxx/python-project-folder-structures/foo/tests/test_foo.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
foo/tests/test_foo.py:1: in <module>
from foo import foo
foo/foo.py:1: in <module>
import cli
E ModuleNotFoundError: No module named 'cli'
!!!!!!! Interrupted: 1 error during collection !!!!!!!
===================== 1 error in 0.05s =====================
Can anyone helps? Why Pytest is not founding my imports? I've tried a bunch of approachs, even with tests folder outside the foo folder (on root level) but the error still persists.
The only way that I've found to fix that is putting a conftest.py file on the code level, with that it would be discovered by pytest. I really don't want to do that, since I want to decouple my testing from my source code.

If anyone strugles with this, I was able to find a solution. I removed the __init__.py file from the foo directoty, giving this structure:
foo
│
├── foo
│ ├── cli
│ │ ├── tests
│ │ └── __init__.py
│ ├── core
│ │ ├── tests
│ │ └── __init__.py
│ ├── tests
│ │ ├── __init__.py
│ │ └── test_foo.py
│ └── foo.py
├── kube
├── requirements
├── ...any-other-non-source-related
└── README.md
Them, on mt test, I got the following now:
import foo
def test_foo():
foo.say_hi()
assert False
I noticed that the behaviour of Pytest is to go from the test until the root dir, identifying packages. Since foo was a package (with a __init__.py file) it was adding it as one on my PATH :)
IMPORTANT
Notice that the import foo statement is regarding the foo.py file, not the foo directory. So, if your file is called boo.py inside the foo directory, the import should be: import boo

Related

Problem importing nested package in Python

Based from a project with the following structure:
.
└── src/
├── main.py
├── PackageA/
│ ├── __init__.py
│ ├── logic.py
│ ├── SubPackageA1/
│ │ ├── __init__.py
│ │ └── util.py
│ └── SubPackageA2/
│ ├── __init__.py
│ └── otherUtil.py
└── PackageB/
├── __init__.py
└── helpers.py
Project structure
It would be possible to import in the file helpers.py the package otherutil.py?
All the combinations I tried until now fail.
If your program is executed from main.py the import in helpers.py should work like this:
from PackageA.SubPackageA2 import otherUtil
Yes, I checked it, main.py:
from PackageB import helpers
print(helpers.HELPERS_UTIL)
otherUtil.py:
OTHER_UTIL = 'test'
helpers.py
from PackageA.SubPackageA2 import otherUtil
HELPERS_UTIL = otherUtil.OTHER_UTIL

No moduled name... with pytest and pytest.ini

Hello I am trying to develop a few tests with python in order to do this I have the following folder structure:
.
├── Pipfile
├── Pipfile.lock
├── src
│ ├── adapters
│ │ ├── __init__.py
│ │ ├── orm.py
│ │ └── repository.py
│ ├── app.py
│ ├── bootstrap.py
│ ├── db.py
│ ├── domain
│ │ ├── __init__.py
│ └── settings.py
└── test
├── conftest.py
├── __init__.py
├── integration
│ ├── test_orm.py
│ └── test_repository.py
├── pytest.ini
└── unit
├── test_account.py
├── test_client.py
├── test_commerce.py
└── test_move.py
The pytest.ini contains:
[pytest]
pythonpath = src
The tests are launched with the following command:
python -m pytest -s -v test/
The problem is about import, in the conftest.py I have the following code:
import pytest
from sqlalchemy.orm import clear_mappers
from adapters.orm import start_mappers
#pytest.fixture
def mappers():
start_mappers()
yield
clear_mappers()
The line from adapters.orm import start_mappers fails if I doesn't set src before like this from src.adapters.orm import start_mappers
but if do that, the file inside orm module fails, because it has the relative import:
from domain import Move, Account, Client, Commerce
instead of
from src.domain import Move, Account, Client, Commerce
but if I use this, of course, another thing fails, and the idea is not to use src. suffix in the src code.
So how can I solve this, in order to avoid installing src as a python package, or fill all folders with init.py or put src. in all places?
Thanks.

Sphinx: unable to import internal modules

My project (written in python 2.7) has a complex structure and most modules are interlinked. There is no direct entry or link to this project to execute. It works as toolbox for other project.
When I tried to use sphinx to create the documentation it is giving error related to "unable to import module.
sample structure:
<workspace>
└── toolbox (main folder)
├── __init__.py
│
├── sub
│ ├── __init__.py
│ ├── sub1.py
│ └── sub2.py
│
├── subpackageA
│ ├── __init__.py
│ ├── submoduleA1.py
│ └── submoduleA2.py
│
└── subpackageB
├── __init__.py
├── submoduleB1.py
└── submoduleB2.py code[from sub import sub1
from subpackageA import submoduleA2 and so on]
Is there a way to configure the index.rst or the config.rst to ignore the import module error and give a output document directory like below:
└── toolbox
│
├── sub
│ ├── index
│ ├── sub1.m
│ └── sub2.m
│
├── subpackageA
│ ├── index
│ ├── submoduleA1.m
│ └── submoduleA2.m
│
└── subpackageB
├── index
├── submoduleB1.m
└── submoduleB2.m
I tried adding system path in config.rst
import os
import sys
sys.path.insert(0, os.path.abspath('../'))
tried ('../..') or ('..')
even hardcoded the project path.
even tried to use the sphinx.ext.autodoc but getting the same import error.
commands used:
sphinx-apidoc -o doc project/toolbox
make html

Can't import subfolders from python module on GitHub

I have a simple python package that I've published on GitHub. I installed the package locally on my machine using pip. I am trying to import a subfolder of the module but I keep getting a ModuleNotFoundError: No module named 'package_folder.subfolder1'
├── package_name/
│ ├── README.md
│ ├── setup.py
│ └── package_folder
│ ├── __init__.py
│ ├── file1.py
│ ├── file2.py
│ ├── subfolder1/
│ │ ├── __init__.py
│ │ ├── file11.py
│ │ └── file12.py
I have the __init__.py files in both directories, so I'm not sure why I am unable to access the subfolder1 files.
I am able to import file1.py and file2.py from the top-level package_folder with from package_folder import file1.py.
In the setup.py you have to include the subfolder in the packages as well. So, in setup.py instead of:
packages=['package_folder']
You have to do:
packages=['package_folder', 'package_folder/subfolder1']

No Module named utils.utils , utils is not a package

I have a Project where there is a python(.py) file inside a Directory say A, under the folder A , I have a file having the code:
from utils.utils import csv_file_transform
where utils is a sibling directory to A
/A
file.py
/utils
__init__.py
utils.py
but, I am getting a error No Module named utils.utils, utils is not a package
I tried adding the path using sys, but its still giving the same error
import sys
sys.path.append('C:/Users/Sri/Documents/newmod/folder/utils')
The code is working fine in ubuntu but not in windows, I am using a windows system
as much as I don't support link only / reference only answers I reckon that your question is answered best by referencing the existing discussions and providing broader context on Python importing mechanism.
Your Requirements
Are you actually working on creating on Python module? If this is the case, it would be good if you could establish a rigorous structure for your module and follow those guidelines.
Consider the following article by Jason C. McDonald on strutting a hypothetical module omission in Python. Given the following structure:
omission-git
├── LICENSE.md
├── omission
│ ├── app.py
│ ├── common
│ │ ├── classproperty.py
│ │ ├── constants.py
│ │ ├── game_enums.py
│ │ └── __init__.py
│ ├── data
│ │ ├── data_loader.py
│ │ ├── game_round_settings.py
│ │ ├── __init__.py
│ │ ├── scoreboard.py
│ │ └── settings.py
│ ├── game
│ │ ├── content_loader.py
│ │ ├── game_item.py
│ │ ├── game_round.py
│ │ ├── __init__.py
│ │ └── timer.py
│ ├── __init__.py
│ ├── __main__.py
│ ├── resources
│ └── tests
│ ├── __init__.py
│ ├── test_game_item.py
│ ├── test_game_round_settings.py
│ ├── test_scoreboard.py
│ ├── test_settings.py
│ ├── test_test.py
│ └── test_timer.py
├── pylintrc
├── README.md
└── .gitignore
Jason would import the modules in a following manner from omission.common.game_enums import GameMode.
Relative Imports
#am5 suggests adding
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
to your __init__.py files. If you study the related discussion you will observer that views on modifying sys.path are diverse. Some may argue that failed imports and errors like:
ImportError: attempted relative import with no known parent package
are actually indication of code smell. As a solution, which I hope you won't find patronising, I would suggest that you solve your importing challenge by:
a) Deciding on your requirements on how "deep" you want to go into modular architecture.
b) Adopting a corresponding recommended architecture with rigorous approach to module structure
c) Refraining from hard-coding any path via sys.path. Appending relative paths, as outlined above, is not a flawless solution but has some merits and may be worth considering.
Other worthy discussions/artefacts
Best structure for the Python project
Sample project by PyPa (GitHub)
Importing files from different folders

Categories