How can I solve the error "Cannot import name 'x' "? - python

I'm trying to convert Keras to a Core ML model but I'm stuck when converting the Python file into a mlmodel.
I'm getting errors when importing submodules of 'coremltools'.
The error that I'm getting is: "python recog.py
Traceback (most recent call last):
File "recog.py", line 3, in
from coremltools import convert
ImportError: cannot import name 'convert'
"
I tried to import the submodules in a different way but nothing worked for me.
I hope anyone can help me!
You can see the Python code, in the sample below:
import coremltools
from coremltools import converters
from coremltools import convert
coreml_model = coremltools.converters.keras.convert('model.h5', input_names='data', image_input_names='data', is_bgr=True, output_names='species')
coreml_model.save('model.mlmodel')

Make sure the name you are trying to import is in the module coremltools.

In the file, coremtools.py double check if the name is same I.e. convert.
Check the location of the coremtools.py file, is it in the main folder of python where python.exe exists?

Related

How do I fix the attribute error in python?

I'm new to python and I'm currently learning objects and graphics. I imported the graphics.py file successfully but for some reason it keeps giving me an attribute error whenever I try to run GraphWin. please see below:
import graphics
win = graphics.GraphWin()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'graphics' has no attribute 'GraphWin'
I'm using Zelle's "Python Programming: An Introduction to Computer Science" and it's been helpful.
please advise.
Is the graphics.py in the same as the file you’re getting the error in?
Also, try
from graphics import *
Instead of
import graphics
i checked out this article and it proved helpful. i just had to create an extension where i added the specific sub-module to the module folder. in this case it's:
from graphics._init_ import *
win = GraphWin()
this solves the attribute error. however, it only works with the from import * formula and not the import formula

How to resolve the Circular import Error?

I am working with phonenumbers module in Python. I am having the issue of circular import. This error omits whenever I run the file from desktop location (C:\Users\AsadA\Desktop). But it raises an error whenever I tried to run this in a particular folder (C:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER ). Please help me!
Sample Code:
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
from phonenumbers import timezone
my_Num=phonenumbers.parse("SAMPLE_NUM")
print(geocoder.description_for_number(my_Num,'en'))
print(carrier.name_for_number(my_Num,'en'))
print(timezone.time_zones_for_number(my_Num))
ERROR:
Traceback (most recent call last):
File "c:/Users/AsadA/Desktop/Python_projects/28-FindingTheNUMBER/phonenumbers.py", line 1, in <module>
import phonenumbers
File "c:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER\phonenumbers.py", line 2, in <module>
from phonenumbers import geocoder
ImportError: cannot import name 'geocoder' from partially initialized module 'phonenumbers' (most likely due to a circular import) (c:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER\phonenumbers.py)
You might probably named your file as "phonenumber.py". If you are importing something in python, please make sure that file name is not same as imported file. If it is same, then it will create an error.
This happens due to same name conflict with imported file as imported file name is also the same. And if this occur then python always give priority to file with current directory you are working.
So, let say your code is something like below.
import xyz
print(xyz.version)
And your file name is "xyz.py". Python compiler now sees that there are two files of same name "xyz.py", one in script folder where python is installed and another in current directory we are working. So, python compiler compiler choose file to import from current directory you are working on.
So, python read first line import xyz, it imports the file from current directory which means it import again this file and start reading it. In that, again first line is import xyz, then it again imports xyz in current folder causing a loop to occur.
This is called as circular loop.
So, in short, changing your file name can solve the problem.
You are importing the module phonenumbers using 'import phonenumbers' and then you are importing the relevant definitions inside that module in the next few lines. They are redundant.
Fixed code:
import phonenumbers
my_Num=phonenumbers.parse("SAMPLE_NUM")
print(phonenumbers.geocoder.description_for_number(my_Num,'en'))
print(phonenumbers.carrier.name_for_number(my_Num,'en'))
print(phonenumbers.timezone.time_zones_for_number(my_Num))
Or something like this:
from phonenumbers import (
parse,
geocoder,
carrier,
timezone,
)
my_Num=parse("SAMPLE_NUM")
print(geocoder.description_for_number(my_Num,'en'))
print(carrier.name_for_number(my_Num,'en'))
print(timezone.time_zones_for_number(my_Num))

Error with importing a class from a module

This is my first time asking a question so please excuse me.
I am following a tutorial and using the code in this folder: https://github.com/ehmatthes/pcc_2e/tree/master/chapter_12/adding_ship_image
I literally replicated this folder in vs code with all the code and the names being used correctly.
For some reason I am getting this error:
Traceback (most recent call last):
File "/Users/sammyawad/Documents/projects/alieninvasion/alien_invasion.py", line 6, in <module>
from ship import Ship
ImportError: cannot import name 'Ship' from 'ship' (/Users/sammyawad/Documents/projects/alieninvasion/ship.py)
Also, I think I have an issue with linting because it is highlighting the pygame init functions for the class even though it works.
Change it to:
from .ship import Ship
import module as instance of class like this...
from ship import Ship as s1

How to import a file from a package which is importing another file from the same package

I've been working on a project where I have a file which needs to call a function from a file in a sub package/directory, which in turn is calling a function from another file in the same sub package. As such, I have a main file which is importing a sub file. This sub file is also importing another sub file which is in the same package.
The first sub file has no issue whatsoever importing the second sub file. The main file also has no issue importing the first sub file. However, when I put it all together and run the main file, Python thinks that the second sub file doesn't exist, which I find strange. I've simplified and visualised my problem with an example below:
I have the following file hierarchy:
test_package\
__init__.py
main_file.py
test_sub_package\
__init__.py
subfile1.py
subfile2.py
main_file code:
import test_sub_package.subfile1
subfile1 code:
import subfile2
subfile2 code:
def get_string():
return ("Hello, World!")
So, I would expect main_file to import subfile2 via subfile1. However this doesn't seem to be the case because I get an error:
Traceback (most recent call last):
File "...\Test\main_file.py", line 1, in <module>
import test_package.subfile1
File "...\Test\test_sub_package\subfile1.py", line 1, in <module>
import subfile2
ModuleNotFoundError: No module named 'subfile2'
I was a little surprised that I got this error before I even attempted to call the functionality in subfile2. Either way, I'm confused why this doesn't work. Am I just doing something stupid here or am I trying to do something Python fundamentally doesn't support. If anyone can give me a solution it would be most appreciated.
I suspect this is probably a duplicate but I couldn't find an answer to my specific problem. So, sorry in advance.
When you import a module into another module from the same directory you must use must use a relative import in subfile1.py you will need to write:
from . import subfile2
Note, that doesn't give subfile 1 access to get_string to use it in subfile1, you would need to either write subfile2.get_string() or import it directly with:
from .subfile2 import get_string
I have tried this out and it works, I hope this helps :)
Note: that, if you are running a python script, and you need to import a module in that same directory, you can just say import module_name. It makes a difference if it is a script you are running, or a module that is being used in some other script. For a detailed explanation as to why see here
(I assume from your error message that you want to run main.py, if this is not the case you will need to change import test_sub_package.subfile1 to from . import test_sub_package.subfile1)
main file should be:
from test_sub_package.subfile1 import get_string
get_string()
subfile1.py
import test_sub_package.subfile2

opencv2 Aruco library modules not working with python

I have compiled the aruco library as stated here github link for aurco library
I have checked it has compiled successfully as i can import it in python without any error and to check i have run the example.py script also it's working but when i wrote this code
import cv2
import numpy as np
import aruco
Dictionary = aruco.getPredefinedDictionary(aruco.PREDEFINED_DICTIONARY_NAME(DICT_5X5_250=6))
aruco.drawMarker(Dictionary,5,250,markerImage,1)
aruco.drawMarker(Dictionary,10,250,markerImage,1)
aruco.drawMarker(Dictionary,20,250,markerImage,1)
aruco.drawMarker(Dictionary,25,250,markerImage,1)
aruco.drawMarker(Dictionary,50,250,markerImage,1)
aruco.drawMarker(Dictionary,100,250,markerImage,1)
aruco.drawMarker(Dictionary,200,250,markerImage,1)
cv2.imshow("markers",markerImage)
cv2.waitKey(0)
cv2.imgwrite(marker.jpg,markerImage)
it throws error
Traceback (most recent call last): File "drawmarker.py", line 7, in
Dictionary = aruco.getPredefinedDictionary(aruco.PREDEFINED_DICTIONARY_NAME(DICT_5X5_250=6))
AttributeError: 'module' object has no attribute
'getPredefinedDictionary'
can someone please let me know what am i doing wrong, is this module not imported in python version of aruco ?
maybe you should try this "aruco.DICT_5X5_250" as parameter, like...
dict = aruco.getPredefinedDictionary( aruco.DICT_5X5_250 )
it worked for me :)

Categories

Resources