I am trying to run a program, whose files are organized as follows, The subfolder of tensorflow_fcn and architecture.py share the same parent directory. In architecture.py, there is a line of code from tensorflow_fcn import fcn8_vgg
The folder of tensorflow_fcn already includes _init_.py
However, running the program gets error message as follows
File "FCN/KittiSeg_pretrained/model_files/architecture.py", line 5, in <module>
from tensorflow_fcn import fcn8_vgg
ImportError: No module named 'tensorflow_fcn'
I do not understand why tensorflow_fcn was not identified by architecture.py.
.../model_files is probably not in your PYTHON_PATH. In Python 3 you can use relative imports for this:
from .tensorflow_fcn import fcn8_vgg
Note the . .
This may also work in 2.7, although you should definitely upgrade if you're still using 2.7 .
Maybe this is related to the relative path of the file and module.
In my work, there is a subfolder "submodules" at the same level as the file, while subfolder "tensorflow_fcn" is under "submodules". So I use this form:
from submodules.tensorflow_fcn import fcn8_vgg
This page may solve your puzzle
http://85608547.blog.51cto.com/2093443/1576759
Related
I'm having trouble with a python package that uses separate modules to structure code. The package itself is working, however when imported from another environment fails with a ModuleNotFound error.
Here's the structure:
Project-root
|
|--src/
| __init__.py
| module_a.py
| module_b.py
| module_c.py
| module_d.py
|--tests
etc.
In module_a.py I have:
from module_a import function_a1,...
from module_b import function_b1,...
from module_c import function_c1,...
In module_c I import module_d like:
from module_d import function_d1,...
As mentioned above, executing module_a or module_c directly from the CLI work as expected, the unit tests I've created in the test directory also work (with the help of sys.path.insert), however if I create a new environment and import the package I get the following error:
>>> import module_a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/<abs_path>/.venv/lib/python3.9/site-packages/module_a.py", line 22, in <module>
from module_c import function_c1, function_c2
File /<abs_path>/.venv/lib/python3.9/site-packages/module_c.py", line 9, in <module>
import module_d
ModuleNotFoundError: No module named 'module_d'
>>>
I've exhausted all ideas how to overcome this, besides combining the code of modules c and d in one file, which I'd hate to do, or rethink the flow so that all modules are imported from module_a.
Any suggestions how to approach this would be greatly appreciated.
Update: It turned out to be a typing mistake in the name of module_d in setup.py. For whatever reason python setup.py install was failing silently or I wasn't reading the logs carefully.
The problem comes down to understanding the basics of the import system and the PYTHONPATH.
When you try to import a module (import module_a), Python will search in order in every directory listed in sys.path. If a directory matches the name (module_a)1, then it runs the __init__.py file is such exist.
When you get an [https://docs.python.org/3/library/exceptions.html#ImportError], it means that there is no directory in sys.path containing a directory with the name asked.
You said for your tests you did something like sys.path.insert(0, "some/path/"), but it is not a solution, just a broken fix.
What you should do is set your PYTHONPATH environment variable to contain the directory where your modules are located, Project-root/src in your case. That way, no need to ever use sys.path.insert, or fiddle with relative/absolute paths in import statements.
When you create your new environment, just set your environment variable PYTHONPATH to include Project-root/src and you are done. This is how installing regular Python modules (libraries) work : they are all put into a directory in site-packages.
1: this changed since old Python versions, it used to be required for the directory to contain an __init__.py file
I've installed two git submodules into my project. Those submodules contain a lot of relative imports, so if you just import something from them, they will not find necessary modules. This problem can be easily solved by adding following line before imports:
sys.path.insert(0, './yolov5_face') # where ./yolov5_face is path to submodule
But that solves problem only for one submodule.
I have following project structure:
/yolov5 # submodule
/yolov5_face # submodule
detect_face.py # script, which imports yolov5_face
track.py # script, which imports yolov5
Submodules are almost identical but have slightly different classes and functions. Script detect_face.py gets loaded first, so I insert './yolov5_face' to sys.path before imports from submodule. Then, in trace.py I insert './yolov5' before imports. So my sys.path after second insertion looks that way:
['./yolov5', './yolov5_face', '/home/sher/Dev/realtime-detection', ... ]
And I have following error:
File "/home/sher/Dev/realtime-detection/track.py", line 14, in <module>
from yolov5.models.common import DetectMultiBackend
File "/home/sher/Dev/realtime-detection/yolov5/models/common.py", line 23, in <module>
from utils.datasets import exif_transpose, letterbox
ImportError: cannot import name 'exif_transpose' from 'utils.datasets' (/home/sher/Dev/realtime-detection/./yolov5_face/utils/datasets.py)
Function exif_transpose resides in yolov5 submodule and its path appears first in sys.path, but it seems like python searches for this import in yolov5_face instead. yolov5_face contains utils.datasets module too, but does not have exif_transpose function. Also all imports from detect_face.py loads just fine.
Is there a way to resolve this problem and use two submodules together?
As per screen print, import shows error in Python 3.7 version, earlier it was working fine in version Python 2.7 and I am using IntelliJ Idea.
If you see, EOC related .py files are in the same folder and have classes which are being called in Main_EOC.py by passing objects which are inter-related. It's amazing to see the red line while importing files from same folder.
Please help me why it's showing such error
"This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.`"
Also, if you see the line which have full path, is not showing error
from EOC_Module.eoc.script.config import Config
Please help me if there is a way to add this full path on top of the code or other option.
The behavior of import path search changed between python2 and python3. The import path always includes the directory from which the main module was loaded, but it no longer includes directories from which modules were imported.
You need to change your import statement syntax as follows, if you want to import a module that lives in the same directory as the module in which you do the import:
# old way, import works if the named module is in this module's directory
import x
# new (Python3) way:
from . import x
For the second part: adding a path so all code can import from a certain directory: if that directory is (and will always be) relative to your main: you can add a few lines in the main module to make it available. Something like this:
import sys # if you haven't imported it already
import os.path
home = os.path.dirname(sys.argv[0])
sys.path.append( os.path.join(home, "EOC_Module/eoc/script") )
# now, you can import straight from the script directory
import EOC_Intraction
When using pycharm the root directory for your python executable is the same as the root directory of your project, this means that python will start looking for files in the root directory with this files:
.idea/
EOC_module/
logs/
reports/
sql/
This is the reason of why: from EOC_Module.eoc.script.config import Config works.
If you execute your code from the terminal with: python3 Main_EOC.py (not pycharm) the root directory for your python will be the same as the one containing the file, all the other imports will work but from EOC_Module.eoc.script.config import Config not.
So you need to make your imports from project directory if you are using pycharm.
I was having some problem when trying to import method from another python class. I am following this guide. When I tried to run the attention.py, I am getting this error message:
File "D:\Desktop\tensorrec-master\examples\attention_example.py", line 8, in <module>
from test.datasets import get_movielens_100k
ModuleNotFoundError: No module named 'test.datasets'
I even tried to change the import statement to:
import test.datasets.py
test.datasets.get_movielens_100k(negative_value=0)
But I am still getting the same error message. Any ideas? Thanks!
Project structure needs to be known to your Python interpreter. And how to do it?
Set PYTHONPATH to base directory of your project
Add init.py file in each directory
Voila! You will be able to import from any directory under your base folder after that
From Import a file from a subdirectory? :
In short, you need to put a blank file named
__init__.py
in the "lib" directory.
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