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
Related
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.
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
So I have a project like this
root/
api/
nest/
nest.py
tests.py
__init__.py
when I run python nest/nest.py -args it works fine, but when I run python nest/tests.py it crashes saying i have an import error in tests.py
ModuleNotFoundError: No module named 'nest.nest'; 'nest' is not a package
my tests.py import looks like this
import unittest
from nest.nest import JsonParser
I also use this JsonParser class in api/api.py module and it works fine
To add to that, if i run tests.py in pycharm it works normally, but if I try in console, it throws this exception
The problem is that your interprerter doesn't know the path in which that module is when running outside the module scope.
A solution could be to add the absolute path of your module in the code:
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
In Python, I want a file in a subpackage to import a sibling subpackage. Like so:
/proj
__init__.py
runner_main.py
/subpackageA
__init__.py
helper.py
/subpackageB
__init__.py
runnerB.py
In runner_main.py, I can call import subpackageA just fine.
However, calling from . import subpackageA fails with error
ImportError: cannot import name 'preprocessing' from '__main__'
This isn't a problem, except I want to import helper.py from runnerB. Calling from .. import subpackageA fails with a similar error.
I don't want to put /proj on my system or Python path; I want it to work as a standalone package. How can I make this simple case work?
Try
from subpackageA import classA, functionB, constantC
It depends on where you run your code. You can import helper.py from runnerB. But If you run runnerB directly, it will gives you same error as you described. However If you run from any py file placed in the parent directory that import runnder module, it will work.
I write some python files like this:
main.py
view/ __init__.py #empity file
MainWindow.py
ListEditor.py
And in each file I wrote those imports:
<main.py>
from view.MainWindow import MainWindow
...
-
<MainWindow.py>
from view.ListEditor import ListEditor
and ListEditor.py don't import any files.
Each MainWindow.py or ListEditor.py defines a class that named same as the file name.
when I run the program from main.py, it works. But when I run from MainWindow.py I got ImportError: No module named 'view'
If I write
from ListEditor import ListEditor
in MainWindow.py, python MainWindow.py will be OK. but python main.py will get error:
ImportError: No module named 'ListEditor'
So, is there a way to make both python main.py and python MainWindow.py get right at the same time?
I'm using python3.4
P.S.
I think I have figured out the problem here. The import command searches a module in sys.path. The sys.path is a group of predefined paths plus the running script path. When I run the code from MainWindow.py, the code import ListEditor just works, but when I run from main.py, the current path is set to the parent path. So I need import view.ListEditor.
Well, there are couple ways to deal with it. #Vincent Beltman's answer is one of it. Or just put these code in the __init__.py file:
import os, sys
path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(path)
Finally, I'm new to python. And I think the import command is quite strange. I thought it should search the files relative to the path of the source file that containing the command, not just relative to the starter file. A starter file may varying and cause troubles like this one.
Try this:
try:
from view.ListEditor import ListEditor # If this one fails
except:
try:
from ListEditor import ListEditor # It will try this one