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.
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
I have the following directory structure:
C:/Automation/Windows/bin/powerconfig/powerconfig.py
C:/Automation/Windows/modules/Pylog.py
I am in the directory of:
C:/Automation/Windows/
When I try to run powerconfig.py file from a windows directory like
C:\Automation\Windows> pyhton \bin\powerconfig\powerconfig.py
I instantly get the error of no module named 'modules.Pylog'
Powerconfig.py contain import statement like
from modules.Pylog import Pylog
All directory contains __init__.py file to consider package
Even I tried to fix error by adding path C:/Automation/Windows/ to sys.path but still I am getting same error.
I don't know how to fix this error.
Try add the following code in Powerconfig.py
import sys
sys.path.append("C:/Automation/Windows/")
from modules.Pylog import Pylog
I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.
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
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