So, I encountered a ModuleNotFoundError when trying to import a module in a subpackage that imports another subpackage under its directory (so it's a subsubpackage to the main directory). I have put empty __init__.py files under both the subdirectory and subsubdirectory. The code was run in Python 3.9.7.
Here's what the structure looks like:
|- main.py
|- subpackage/
|- __init__.py
|- submod.py
|- subsubpackage/
|- __init__.py
|_ subsubmod.py
The code
In main.py, I have:
from subpackage import submod
def main():
x = submod.test_func(3)
print(x)
if __name__ == 'main':
main()
and in submod.py, I want to import subsubmod.py under subsubpackage/, so I have:
from subsubpackage import subsubmod
def test_func(a):
return subsubmod.addone(a)
and finally, in subsubmod.py:
def addone(x):
return x+1
The error message:
Now if I run main.py, I got
Traceback (most recent call last):
File "/Users/anonymous/test/main.py", line 1, in
<module>
from subpackage import submod
File "/Users/anonymous/test/subpackage/submod.py",
line 1, in <module>
from subsubpackage import subsubmod
ModuleNotFoundError: No module named 'subsubpackage'
My question and confusion
I'm not sure what I have done wrong. I realized that submod.py can be run separately, so it seems that the issue occurs when the import goes down more than one subdirectory? I wonder if there's a way around this issue, or should I just use a different structure to organize my scripts.
Putting a dot before the package name worked for me.
from .subsubpackage import subsubmod
Looks like when you are in a package if you don't use relative import it will look for your packages somewhere else.
You can find more information here:
Python documents about import system
StackOverflow question about relative imports
This question keeps showing up on Stack Overflow and I can understand why. So today I took the time to create a nested project with 1 top module and 2 nested sub modules both with classes in them. And another class was also created in the top module folder. This is also considered a module by python. But it's in the same folder as the top module.
The skeleton nested module project is now live on Github at:
HdlHelpers Nested Modules Skeleton Project
Related
I have the following file/folder structure:
testpackage (folder)
src (folder)
__init__.py
module1.py
tests
__init__.py
test_module1.py
just for clearance: the "module1.py" is under the "src" folder which is under the "testpakcage" folder.
"tests" is also under the "testpakcage" folder - same level as the "src" one.
module1.py has a class named "class1" as so:
class class1:
def method1 (self):
print('i am method1')
in test_module1.py I want to run tests on the above module. this is it's contents:
import unittest
from testpackage.src import module1
t = module1.class1()
t.method1()
this package is not installed, and I don't instead to install or submit it anywereh, I'm just trying to find the best structuring practice for me, for future packaging creation.
problem is: when I run the following either from the "tests" or "testpackage" folder:
/usr/bin/python3.6 -m unittest discover
I get the following error:
E
======================================================================
ERROR: test_module1 (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_module1
Traceback (most recent call last):
File "/usr/lib64/python3.6/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib64/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
something similar also happens when I just try to run "test_module1.py" from the "tests" folder:
Traceback (most recent call last):
File "test_module1.py", line 5, in <module>
from testpackage.src import *
ModuleNotFoundError: No module named 'testpackage'
so I tried changing the "import" line with a few alternatives but none of them work. each one of those was a different attempt (not all of them at once):
from testpackage.src import *
import testpackage.src.module1 as module1
import ..src.module1
from ..src.module1 import class1
searching stackoverflow I found solutions that worked for some but not for those using python 3 and above - which is my case.
any suggestions? I think what I'm trying to do is rather simple and I'm missing something really basic here.
I'm using python3.6 by way
I'm wondering if you saw different errors for some of those different import attempts you made. Not sure if this will solve your problem, but this is one way it would generally be accomplished.
First, I am not sure what the top level of your package is supposed to be. Do you reference testpackage in any of the code in the src folder? If so, that top folder should also contain an __init__.py file.
Now, at the very top of your test_module1.py file, add:
import sys
pkg_dir = ".." # if "src" if the top level folder else "..." if testpackage is the top level folder
sys.path.append(pkg_dir)
Note you must change pkg_dir depending on your module's structure.(which I cannot tell from your question).
What this code does is add the folder containing the top level folder of your package to the python import search tree. This can solve problems that the relative import in your example file will not: if the files in your module use module-level imports (e.g. import testpackage.src.module2 in module1.py). This is common in packages with multiple submodules that cross-import.
I am trying to make my own package so that I can use the files in a different folder. This package contains a few different modules and then the main module that imports all the others inside it. For example:
Folder
|- main.py
|- other.py
|- something.py
|- __init__.py
Inside the main.py I have the imports:
import other
import something
and it works just fine when running the file itself; however, I added the __init__.py file and tried to import it into a different folder. The package is recognized, but the main.py gives me the following error:
Exception has occurred: ModuleNotFoundError No module named
'univariate'
File "C:...\stats.py", line 8, in
import univariate
File "F:...\testing.py", line 7, in
from stats import stats
For clarification, the actual main file is called stats.py. This is my first experience trying to make a package so I might be missing something. Thank you.
You need to change your imports into relative imports
import .other
import .something
or to change it to absolute imports rooted to your project folder
import x.y.other
import x.y.something
you can read here about the imports
When you have a module that you're trying to import you don't need the ".py" part.
Having a folder with a init.py file (even a blank one) means that a project that contains that folder can import from it.
/myproject
| - /mymodule
| - |- stats.py
| - |- other.py
| - |- something.py
| - |- __init__.py
| - main.py
then in main.py all you need to do is import mymodule or from mymodule import stats
I always hate to FTFM someone, but here's a link to how to build packages from the official documentation. But, where this really starts to shine is when you need to package your module so that someone else can run it Digital Ocean has a pretty good tutorial here.
I have a project directory structure like below
|project
|-__init__.py
|-src
|- __init.py__
|- features
|- __init.py__
|- clean_data.py
|-notebooks
|- notebook.ipynb
The main directory is called project under which I have two directories- src and notebooks.
I want to import the module clean_data.py under features directory (which is under src) in my notebook.ipynb file.
I tried this:
from ..src.features import clean_data
since all directories are serving as package with init.py file in each of them.
But it throws an error. Have spent quite a lot of effort in trying to figure this out but not sure why I am getting the error. As per this article too, I seem to be accessing the module correctly
mportError Traceback (most recent call last)
<ipython-input-23-11fd29e06b4c> in <module>()
----> 1 from ..src.features import clean_data
ImportError: attempted relative import with no known parent package
This is a part of my code, look at this:
from domain_pricing.domains import *
from domain_pricing.conversion_rate import *
I'm importing domains.py and conversion_rate.py from domain_pricing folder.
What you should do is:
from src.features import clean_data
from src.data import another_module
You do not need . or .. as Unix-Based systems pathing directories. You need to call the folder directly.
I am playing around with the AIMA python project, but I'm having trouble with importing the logic.py file into main.py. The following is folder structure:
project/
aima/
__init__.py
utils.py
logic.py
main.py
I added the folder to my python path variable. Every time I do
# main.py
import aima.logic as logic
I get this error:
File "main.py", line 2, in
import aima.logic as logic
File "/project/aima/logic.py", line 34, in
from utils import (
ImportError: No module named 'utils'
I thought this was strange since logic.py imports the utils file it should be fine since they are under the same directory.
I tried searching for answers, but most of them mention adding to python module search path and adding __init__.py and do not work for me.
Trying this may be good
from project.aima import logic
I have checked as many SO pages as I could and tried everything I found, but none have been successful. I also checked the PEP page regarding importing and tried every example, none of which worked.
I have a tests folder with unit tests in them, and I need to import the module I want to test. The modules are in a folder called 'src' which is next to the src folder.
The folders/files look something like this:
Project /
src /
stringbuilder.py
__init__.py
tests /
stringbuilder_test.py
__init__.py
main.py
__init__.py
I have tried everything I could: adding __init__.py to every folder making it a module including the project's main folder.
import src.module_to_test
from ..src.module_to_test import function_to_test
from ..src import module_to_test
I have tested all other combinations all of which have failed. I am starting to think there must be something wrong with either my settings or understanding-- I thought this was suppose to be simple.
If I am making any obvious errors please let me know.
from tests import stringbuilder
Error Message:
$ ./stringbuilder_test.py
Traceback (most recent call last):
File "./stringbuilder_test.py", line 14, in <module>
from tests import stringbuilder
ImportError: No module named tests
The same error occurs for (but tests = src):
from src import stringbuilder
If scripts are executed from inside a package, then various hacks need to be employed to get the imports to work properly. Probably the most common of these is to manipulate sys.path:
import sys, os
sys.path.insert(0,
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src import stringbuilder
del sys.path[0]
There is no way to achieve this using normal import statements.
A generally better solution is to simply avoid running scripts inside packages altogether. Instead, put all scripts outside the package in a containing directory. Given that the directory of the currently running script is automatically added to the start of sys.path, this will guarantee that the package can be always be directly imported, no matter where the script is executed from.
So the directory structure might look something like this:
project /
package /
__init__.py
src /
__init__.py
stringbuilder.py
tests /
__init__.py
stringbuilder_test.py
main.py
test.py
The test.py script can then import its tests like this:
from package.tests import stringbuilder_test
And stringbuilder_test.py can import the src modules like this:
from package.src import stringbuilder
from folder import my_module
If there is a __init__.py file in folder.
Add that folder to your PATH.