Python unittest import problems - python

I'm having a tough time understanding packages, and specifically how to use unittest with packages. I looked at this question () but the correct answer to that question didn't solve my problem. I have the following structure:
model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
With the following files/imports:
model/__init__.py:
import model.exceptions.exceptions
import model.boardmodel
model/exceptions/__init__.py:
contains nothing
model/test/__init__.py:
contains nothing
imports inside boardmodel.py::
from model.exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
imports inside test_boardmodel.py:
import unittest
from model.boardmodel import Board, Ball, Wall
from model.exceptions.exceptions import ProximityError
I place myself in the model-directory and I run python -m unittest test.test_boardmodel.py. I get the following message:
ERROR: test_boardmodel (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_boardmodel
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/sahandzarrinkoub/Documents/Programming/pythonfun/BouncingBalls/balls/src/model/test/test_boardmodel.py", line 3, in <module>
from model.boardmodel import Board, Ball, Wall
ModuleNotFoundError: No module named 'model'
I'm a bit lost with how the imports work and what location the modules/packages are looked for when an import statement is executed. Why isn't model found?
I shall add that if I remove model. from all the imports listed, the tests work, but I can't use the package from "outside" anymore:
src
|-- visual.py
|
|-- model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
inside visual.py:
import model
from model.boardmodel import Board

I was facing the same issue, being able to import some modules from several files, but not from a test file, so I saw this solution:
If you have test/my_test.py, tests should be run as:
python -m test.my_test
After that, I imported what I wanted and got no errors.

Try adding the following above your import statements:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

I believe the standard package structure is
myproject
├── myproject
├── tests
└── scripts
If you want to run the test without installing the package, run them from the top myproject folder so that import myproject will succeed in your tests. (Similarly for the scripts.) For this to work, use absolute imports or explicit relative imports in myproject.

Related

Importing a file with functions - attempted relative import with no known parent package erorr

there is my project hierachy:
#__init__.py are empty files
folder
|-- globalFunctions.py
|-- __init__.py
|-- monitor
|-- file.py
|-- __init__.py
I'm tring to import functions from globalFunctions.py, when I'm in the file.py file.
I have tried to import it with
from .. import globalFunctions
but I'm getting
ImportError: attempted relative import with no known parent package
Do you know how to make it work?

What's the best practice to make a directory an importable package?

I created a few utility functions, all put into a folder, and later I created some scripts which import the functions in that folder, please see the following the structure of files,
root
|-- myutils (folder with util python files)
| |-- __init__.py (empty)
| |-- utils1.py
| |-- utils2.py (utils1 is imported by utils2)
|
|-- myscripts
| |-- hello.py (try to import utils1 and utils2)
Now the problem is I cannot import utils1/utils2 inside hello.py, here is what I tried
# This is hello.py
import sys
sys.path.append("root") # so that we can search root and find myutils
import myutils # import myutils, this is OK and no errors
x = myutils.utils1.foo() # ERROR: module 'myutils' has no attribute 'utils1'
How could I fix the error and what's the best practice for my use-case here?

Python project structure error while importing

I just started coding in python and I encountered some errors. My text editor (vscode) doesn't show any error while importing the module but whenever I run the code I have encountered it.
So basically, my directory tree looks like this
lib/
|
|-- core/
| |-- module.py
| |-- __init__.py
|
|
|-- python/
|-- server.py
|-- worker.py
|-- __init__.py
When I import module.py from python/server.py and use
import lib.core.module
I got an error: No module name lib.core. I tried adding "." but it doesn't work
Try:
from .lib.core.module import *

Importing modules from subpackage

My project has this kind of structure:
mypackage
|
|-- __init__.py
|
|-- file.py
|
|-- subpackage
|
|-- __init__.py
|
|-- function.py
How do I import the modules in the subpackage from file.py?
I tried a couple of things like simply import subpackage or from subpackage import function but all of them lead to ModuleNotFoundError: No module named 'subpackage'.
from . import subpackage has no error message but I don't know how to access the module with that.
I solved it by using:
from mypackage.subpackage import function
Just use
from subpackage import function
You propably are not in mypackage directory

Python import issue for different subpackages

I am trying to implement following hierarchy.
My final goal is myscpt.sh should run from any folder
But I am getting import errors.
What is the best approach to implement such type of hierarchical architecture?
FooSoft
|
|-- foo/
|
|-- test.py
|
|-- common/
|
|-- utils/
|
|-- api/
|
|-- scripts/
|-- myscript.py
|
|-- bin/myscpt.sh
|
|-- etc/foo.conf
bin/myscpt.sh
/usr/bin/python /path/FooSoft/foo/script/myscript.py /path/FooSoft/etc/foo.conf
foo/script/myscript.py
from ..common import *
from ..utils import *
from ..test import *
.
.
<Doing some stuff>
I am using .. import in most of modules to avoid absolute path.
Typically I resolve import errors by always using the package root as a reference.
First, I would include a setup.py file in the project root, and add in a minimal setuptools wrapper.
python setup.py develop
Then you don't have to worry about where you run the script from. Your imports become as follows:
from foo.common import *
from foo.utils import *
from foo.test import *
Explicit relative imports with leading dots like from ..common import anything can be used only from a code that has been imported as a submodule of the package e.g. foo.scripts, but not from the the code imported as __main__ script, even if the script path contains .../foo/scripts/.... The main script must use absolute imports or it can be used as module by something like python -c "from foo.scripts import myscript; myscript.run()". (install the foo or use PYTHONPATH=/path/FooSoft). See Intra-package References in the docs or a similar question.

Categories