I have the following project structure
Project Structure
In utils.py I am importing a parse function form parser.py file like this
from logica.parser.parser import parse
In builder.py I am importing a function from utils.py that uses the parser function like this
from utils.utils import utils
When trying to run all this stuff I get a ModuleNotFoundError:
ModuleNotFoundError: No module named 'logica'
Do you guys have any ideas on how to resolve the issue?
Thanks
In Python you can only import modules installed via pip or files stored in the same directory of the current file.
To solve this problem you can do this:
import sys
sys.path.append(<path-of-the-module>)
I would suggest to read also this.
Related
I am trying to import a class named DataHandler into my test_data_handler file. I am doing this by stating from handles.data_handler import DataHandler. However, when I do this I get a error saying
No module named 'handles'
If anyone knows how to get round this issue it would be greatly appreciated!
You need add __init__.py file inside folder handles.
The __init__.py file lets the Python interpreter know that a directory contains code for a Python module.
import sys
sys.path.insert(1, '/path/to/application/app/folder')
from handles.data_handler import DataHandler
Importing files from different folder
So I have a module called gann which itself has a number of modules, the problem is that if I run gannController.py directly it fails.
I have the following file structure(some files omitted):
----convert
__init__.py
convert.py
----AI
__init__.py
----gann
gannController.py
----model
__init__.py
modelController.py
----util
now I want to use the gannController.py in the convert.py file.
from AI.gann import gannController
in convert.py does import gannController, however it crashes on the first line which is
from model import modelController
which does work if I run gannController.py directly. As it gives the error:
ModuleNotFoundError: No module named model
So I guess that it's because a submodule uses a submodule of it's own that I cannot run this. Anybody know how to fix this? It's worth noting that I would prefer not to pollute my convert namespace with all of the stuff in model and that I have omitted a few other modules that have the same situation (inside util)
Change your import to be relative:
from .model import modelController
I am trying to import a module that imports a module. But that doesnt work. This is my folder hierarchy:
/Project/
-test.py
/recognizer/
-__init__.py
-tools.py
-feature_extraction.py
I am importing tools.py inside of feature_extraction.py. If I run the feature_extraction.py it all works like it should. Now I want to import the feature_extraction.py from the test.py, but that doesnt work. I get this error ModuleNotFoundError: No module named 'tools'.
Inside the test.py I am importing like this from recognizer import feature_extraction that works like it should I think. Inside of feature_extraction.py I am importing just like this import tools. This should work I think, but it doesnt.
You should do this way.
from . import tools
If you import tools from feature_extraction.py file. it will lookup main directory which test.py lays.
try to import in feature_extraction tools like this:
import recognizer.tools
I am currently using Python 3.7.
My project has the following structure:
runner.py
pkg1/
__init__.py (empty)
mod1.py
mod1_helpers.py
In mod1.py:
import mod1_helpers
# call some functions in mod1_helpers.py
In runner.py:
import pkg1.mod1
When I run runner.py, Python threw the following error:
File "..\pkg1\mod1.py", line 1, in <module>
import mod1_helpers
ModuleNotFoundError: No module named 'mod1_helpers'
The issue resolves when I manually add the path to pkg1 to sys.path in runner.py.
Could someone please advise the proper way to resolve this issue?
Since they're in the same directory you have to prefix import of mod1_helpers with a .
try this:
import .mod1_helpers
I got a strange issue and have no idea about the reason.
As you can see from the picture, folder fuzzier and parser are under the same parent folder, and both of them have the file __init__.py (both empty because I am not using from xxx import *, and code is based on Python 3.6).
And in another module (under the same parent folder with fuzzier and parser), there is a file doing some import like this:
import fuzzier.jison
import parser.annoying_char
The first line is good, but the second line is with an error ModuleNotFoundError: No module named 'parser.annoying_char'; 'parser' is not a package
I wasted hours on this and wish someone can help with this, Thanks!
parser is an in-built library in Python.
Python is trying to find annoying_char inside that library instead of your module.
You should use some other name.
Source - https://docs.python.org/2/library/parser.html