I'm trying to run Tensorflow's translate.py from a python console rather than through bazel -build, but I get an error at these two lines:
from tensorflow.models.rnn.translate import data_utils
from tensorflow.models.rnn.translate import seq2seq_model
ImportError: No module named translate
I've checked the folder to see that the "init.py" file is there, but python seems to think there is no such module as translate.
How can i fix this?
The best way to do this is to navigate to folder containing the translate module and running it. You can also download the translate module to any other place and run it. However, don't forget to change the above lines to:
from translate import data_utils
from translate import seq2seq_model
I resolved this issue by removing all the from tensorflow.models.rnn.translate statements, leaving just
import data_utils
import seq2seq_model
in translate.py and
import data_utils
in seq2seq_model.py.
Related
I have installed kornia and imorting it like,
from kornia.color import *
import kornia.augmentation.functional as F_k
import kornia as K
but the second line is giving error
ModuleNotFoundError: No module named 'kornia.augmentation.functional'.
Also, this is my directory structure.
But I getting error
ModuleNotFoundError: No module named 'FewShot_models'
when I try to import from FewShot_models.manipulate import *.
I am following a code from github and trying to implement that.
kornia.augmentation.functional was removed in version 0.5.4 and the most of the functions are available through kornia.augmentation.
Regarding your second question, you need to add empty file named __init__.py to FewShot_models directory. Check this answer for details about __init__.py.
I have two modules in the same directory:
PDSC2.py and db_layer.py
I want to import a class named DBLayer from db_layer.py so I write:
from db_layer.py import DBLayer
But I get an error:
ModuleNotFoundError: No module named 'db_layer'
Does somebdy have an idea what i'm doing wrong?
first of all assume that this python files in the same directory and then remove extension from your code.
from db_layer import DBLayer
or:
from db_layer import *
Is the directory in a place where python searches for modules, python path? Do you have a __init__.py in the directory (it can be blank)?
You need to paste the program file db_layer.py in the \Python\Python36-32\Scripts directory
, then use from db_layer import DBLayer or from db_layer.py import DBLayer to call the desired class in the python program .
Actually sometimes changing the directory of the called module to \Python\Python36-32\Scripts solves these types of problems easily.
This is the solution that worked for me:
import sys
sys.path.append("C:\\Users\\carmel.han\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins/filterparcel")
from db_layer import DBLayer
I am not a proficient Python coder, hence this might be a basic question:
In my main python code, I load a python code file using (dynamically)
import imp
model = imp.load_source('name','c:/modeldir/modelfile.py')
modelfile.py does an import on the top:
from MyLib import MyLib
MyLib.py is in the same folder as modelfile.py
I get:
ImportError: No module named 'MyLib'
I have also tried:
import os
os.chdir('c:/modeldir')
just before the imp.load_source, did not help.
EDIT:
I am using Python 3.5.2
I've added an empty __init__.py file in 'c:/modeldir'
How to solve this?
The following made it work
sys.path.append('c:/modeldir')
This adds the folder to the python import path and then MyLib can be found
It is hard to find a title for this question and hopefully this thread is not a duplicate.
I was writing that long script in Python 2.7 (using PyCharm 2016.2.2) for a project and decided to split it in different .py files which I could then import into a main file.
Unfortunately, it seems that when importing a module (e.g. numpy) earlier in the code does not mean that the .py files that are imported below will have knowledge of that.
I am new to python and I was wondering whether there is an easy workaround for that one.
To be more clear, here is an example structure of my code:
Main.py (the file that is used to run the script):
import basic
numpy.random.seed(7)
import load_data
basic.py:
import pandas
import numpy
etc...
load_data.py:
raw_input = pandas.read_excel('April.xls', index_col = 'DateTime')
etc...
The second line of Main.py would cause an error "NameError: name 'numpy' is not defined", meaning the numpy that was imported in basic.py is not passed to the Main.py.
I guess that a similar error would occur for the code in load_data.py since 'pandas' would not be a defined name.
Any ideas?
Thanks.
The numpy module imported in basic.py has a reference defined only withing basic.py scope. You have to explictly import numpy everywhere you use it.
import basic.py
import numpy
numpy.random.seed(7)
import load_data.py
I have installed PyML package in order to use some machine learning algorithms, and according to the tutorial, my installation is successful.
I try to run a python script which includes the following line to import modules from PyML
from PyML import datafunc,svm,assess,modelSelection,ker
However I get the error message above saying
File <stdin>, line 1, in <module> ImportError: cannot import name
datafunc
cannot import name datafunc`. From terminal I check every module by saying
from PyML import datafunc,
from PyML import svm,
from PyML import ker
I only get error message for datafunc. The PyML library is under the site-packages folder of Python 2.7.
I check this question here Python error: ImportError: cannot import name Akismet, but I could't see how it will help my problem.
Do you have any idea why Python imports some modules but does not import this one?
In PyML-0.7.13.3, the datafunc module exists in PyML/containers directory.
So it seems that you can import the module as follows:
from PyML.containers import datafunc
Howerver, it raises an error beacuse the datafunc module uses
undefined classes BaseVectorDataSet and SparseDataSet.
Thus you need to modify the source of PyML
in order to use datafunc module.
First, prepend the following two lines to PyML/containers/datafunc.py
and re-install the PyML library.
from PyML.containers.baseDatasets import BaseVectorDataSet
from PyML.containers.vectorDatasets import SparseDataSet
Then you can import the modules as follows:
from PyML import svm, modelSelection, ker
from from PyML.containers import datafunc
from from PyML.evaluators import assess
BTW, I recommend that you use more documented and tested machine learning library, such as scikit-learn.