I have created a package containing sub-folders and I would like to include a parent module from a sub-package module.
I have tried to follow the project structure suggested here https://docs.python-guide.org/writing/structure/ and attempted to replicate the step-by-step procedure as listed here http://zetcode.com/lang/python/packages/ but it seems that I am missing something obvious about python's package system
Here's my project structure
watches/
-- ...
-- watches/
---- __init__.py (empty)
---- Logger.py
---- main.py
---- db/
------ __init__.py (empty)
------ EntryPoint.py
Logger.py contains a single class :
class Logger:
...
I try to import Logger.py's class and methods from db/EntryPoint.py as follow :
from watches.Logger import Logger
class EntryPoint:
...
Then, I want to wrap-up everything in main.py as follow:
from db.EntryPoint import EntryPoint
if __name__ == "__main__":
t = EntryPoint("local")
and finally, when I try to execute main.py as follow python3 main.py (so I am located in watches/watches directory as you can guess), I guet the following error stack trace :
Traceback (most recent call last):
File "main.py", line 1, in <module>
from db.EntryPoint import EntryPoint
File "some/absolute/path/watches/watches/db/EntryPoint.py", line 4, in <module>
from watches.Logger import Logger
ModuleNotFoundError: No module named 'watches'
Every import will be relative to the location where the script is being run, in your case, main.py.
So, the point of view of your program is:
-logger.py
-__init__.py
-db/
---__init__.pt
---EntryPoint.py
The program is not aware that he is an module called watches, so if you want to import the logger.py in your main, simply do:
from Logger import Logger
Or move your main to the parent folder.
Related
Setup
test/
main.py
pkg/
a.py
__init__.py
main.py contains:
import pkg
pkg.a
__init__.py contains:
from . import a
main.py can be run without errors.
Question
Changing the content of __init__.py to
import a
gives the following error when running main.py:
Traceback (most recent call last):
File "C:/Users/me/PycharmProjects/test/main.py", line 1, in <module>
import pkg
File "C:\Users\me\PycharmProjects\test\pkg\__init__.py", line 1, in <module>
import a
ModuleNotFoundError: No module named 'a'
Interestingly, __init__.py can be executed directly with python __init__.py without errors.
What's going on?
When you run a python script, it's parent folder is added to sys.path
run main.py: sys.path[0] = '../test'
run init.py: sys.path[0] = '../test/pkg'
Your case: You try to "absolute-like" import a in __init__.py but the parent folder of a.py - which is '../test/pkg' - is not in the sys.path when you run main.py. This is why you get an error. However, your absolute import is incomplete as it should always start at the top level folder, e.g.
from test.pkg import a
Final answer to your question: You don't have to use relative imports!
See: PEP-8: Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path).
And keep in mind that relative imports don't work in a top-level-script when __name__ = "__main__", but from imported modules only.
You can learn more about absolute and relative imports here:
Absolute vs. explicit relative import of Python module
https://realpython.com/absolute-vs-relative-python-imports/
I suppose you are using Pycharm? Then that's one of the confusion cause.
For example, let's say your directory looks like this
project1
p1.py
test/
__init__.py
main.py
pkg/
a.py
__init__.py
If you run (F10) the main.py your default working directory will be project1/test, which does not contain the a.py so import a will not find anything.
But if you run (F10) the pkg/__init__.py your working directory will be project1/test/pkg which has the a.py, and it works like what you tested.
So in these situation, if you use from . import a it will look for the directory that file is, project1/test/pkg in this case, which will always work regardless your working directory.
I'm trying to run my unit tests for a python module from a separate test script. Here is my file structure
- root
|- modules
|- a_module.py
|- test
|- test_a_module.py
|- main.py
The main.py looks like this:
import unittest
loader = unittest.TestLoader()
suite = loader.discover(start_dir='./test', pattern='test_*.py')
runner = unittest.TextTestRunner()
runner.run(suite)
And here are a_module.py and test_a_module.py:
# a_module.py
def something():
return True
# test_a_module.py
import unittest
from ..modules.a_module import something
class TestSomething(unittest.TestCase):
def test_something(self):
self.assertTrue(something)
When running python3 main.py I get the following error.
======================================================================
ERROR: test_a_module (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_a_module
Traceback (most recent call last):
File "/usr/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/home/schu_max/root/test/test_a_module.py", line 3, in <module>
from ..modules.a_module import something
ImportError: attempted relative import with no known parent package
----------------------------------------------------------------------
I'm relatively new to python and don't have a clue on how to solve this issue. And other questions/answers on SO weren't that helpful either. So how can I get this to work?
There are several ways to make your tests run. Now that they are import packages try
python -m unittest -v in the root package folder, after removing the two dots from the relative import path in test/test_a_module.py. Will work too with python main.py from the same folder. If you issue python -m unittest -v outside root and you prefix the relative path with root it will work too. There are probably other ways.
Just beaware that the relative import paths in the test files are affected by the folder from where you run your test script.
To stay coherent, run your tests always from the same place, and put all your tests inside test/ where you can refer all you root subpackages by relative path, without the need to mention the `root* package name, which is nice because you might want to rename it in the future.
My current projects are structured like so.
python/
__init__.py
project_1/
__init__.py
program_1.py
project_2/
__init__.py
program_2.py
project_3/
__init__.py
program_3.py
I wanted to import a class from project_3/program_3.py, called INFO, from both project_1/program_1.py and project_2/program_2.py. In both my program_1.py and program_2.py, I've tried the following import statements that didn't work.
Edit - I typically "cd" into project_1 and run my program_1.py from there.
from project_3.program_3 import INFO
Traceback (most recent call last):
File "./program_1.py", line 43, in <module>
from project_3.program_3 import INFO
ImportError: No module named 'project_3'
from python.project_3.program_3 import INFO
Traceback (most recent call last):
File "./program_1.py", line 43, in <module>
from python.project_3.program_3 import INFO
ImportError: No module named 'python'
The only way for me to import class INFO into program_1.py and program_2.py is by copying program_3.py in both program_1 and program_2's folder. My import in program_1 and program_2's statement would become:
python/
__init__.py
project_1/
__init__.py
program_1.py
program_3.py
project_2/
__init__.py
program_2.py
program_3.py
project_3/
__init__.py
program_3.py
from program_3 import INFO
This is quite inconvenience because now I have to update program_3.py in all 3 locations. Assuming my the way I structured my project folder is retarded:
What's the correct/best way to structure my folders so that I could call program_3.py from program_1/2.py?
If there's nothing wrong with my current structure, how do I correctly import the INFO class into program_1/2.py?
I have read the python documentation but it didn't say anything regarding importing classes from one subfolder to another. I also looked at another post from Stack Overflow as well but it didn't have the same structure as mine.
Update 1 - It's important to note that I'm merely using project_1/2 as folder to organized my projects. I originally had init.py in all my folders because I have no clue what I'm doing. The only module/package that I wanted to import in my project_1/program_1.py is from project_3/program_3.py. Example:
python/
project_1/
program_1.py
project_2/
program_2.py
project_3/
__init__.py
program_3.py
When using submodules, you need to use relative imports. In the example provided, importing program_3 from program_1 should be :
from ..project_3 import program_3
and then you can use the INFO class: program_3.INFO.
This works if your executable file is outside the script, that is the file which uses the module python in the example should be outside the python module.
If parts of the python package are executed as a script, the -m option should be passed to the interpreter:
python3 -m python.project_1.program_1
I hope this helps.
The previous solution provides me the same error.
I found a fix by using sys:
import sys
sys.path.append('../')
from project_3 import program_3
...
This way, you setup 'python' folder as reference (in your case it's the parent folder but you can select the path you want)
I hope it helps,
My package structure is:
main.py
mapp/
__init__.py
core/
__init__.py
tobeimported.py
test/
__init__.py
(test modules)
utils/
__init__.py
blasttofasta.py
The file blasttofasta.py is executed as script.
blasttofasta.py looks like:
import mapp.core.tobeimported
def somefunc():
pass
if __name__ == '__main__':
pass
But Exception occurs:
Traceback (most recent call last):
File "utils/blasttofasta.py", line 5, in <module>
import mapp.core.tobeimported
ImportError: No module named mapp.core.analyzers
How to import tobeimported module? I run the blasttofasta.py from top directory (where main.py is)
EDIT: Maybe better question is: How to get mapp package to the sys.path? Because script file only see its own directory but not the package directory.
Thank you
If I want to including blasttofasta.py or run it as script simultaneously mos important is to have directory containing mapp package in sys.path.
This worked for me:
Before importing mapp (or other module from this package) I wrote into blasttofasta.py:
import os
os.sys.path.append(os.path.dirname(os.path.realpath(__file__))+ '/../../')
This append mapp package path and I can run it as script. On the other side is no problem with is included in another package.
Follow the absolute structure to import.
To import blasttofasta.py in tobeimport.py
ToBeimport contents
from myapp.utils import blasttofasta
Your structure is good.
Two things need to happen:
Your map directory needs a __init__.py file.
You can simply do this (naively):
$ touch /path/to/map/__init__.py
/path/to/map needs to be in sys.path
Please read: http://docs.python.org/2/tutorial/modules.html for more details.
This is my folder structure:
src/
__init__py
Lowlevel/
__init__.py
ModuleToCheck.Py
Test/
__init__.py
ModuleToCheck_test.py
(__init__.py are empty files)
Now I want to import ModuleToCheck.py in ModuleToCheck_test.py
How am I able to do this without appending anything to sys.path?
Update:
from ..Lowlevel import ModuleToCheck leads to:
src$ python Test/ModuleToCheck_test.py
Traceback (most recent call last):
File "Test/ModuleToCheck_test.py", line 6, in <module>
from ..Lowlevel import ModuleToCheck
ValueError: Attempted relative import in non-package
The following is from http://docs.python.org/tutorial/modules.html#intra-package-references
Note that both explicit and implicit
relative imports are based on the name
of the current module. Since the name
of the main module is always
"__main__", modules intended for use
as the main module of a Python
application should always use absolute
imports.
You're running your module ModuleToCheck_test.py as the main module, hence the exception.
One solution is to create a test.py module in your src directory containing the following:
import Test.ModuleToCheck_test
You can then run that module using python test.py