Error with importing a class from a module - python

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

Related

Import function from a cython python module not working

I have made a module:
However I got this error when I import the count_kmers function:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'kmer_cython' has no attribute 'count_kmers'
I import my stuff like this:
from Seq_utils.fasta_parser import parse_fasta
from kmer_cython import count_kmers
from Alphabet.alphabet import iupac_dna
My function was defined like this:
It is a simple function that worked fine when I had it spread in the middle of other functions in my directory. Then I decide to organize it and now I wish to run it, but I can't.
I have set the PYTHONPATH in my terminal and other codes that I made a module are working.
Any tip to solve this problem?
Thank you all for your time.
Paulo

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))

Import class from another file

I know this question has been asked before, but none of the given answers worked. Say I have a file named Test.py with this code:
class Test():
def __init__(self):
print('Instance Created')
I want to use this class in another file, say Test2.py.
I used the path to the file to get the class, but the message attempted relative import with no known parent package pops up
.
I am using IDLE on Mac OS X.
The path to the file is /Users/*name*/Desktop/Code/Test.py, where *name* is my username for this device.
Code in Test2.py:
from .Users.*name*.Desktop.Code.Test import Test
When I run the program, this message comes up:
Traceback (most recent call last):
File "/Users/*name*/Desktop/Code/Test2.py", line 1, in <module>
from .Users.owen.Desktop.Code.Test import Test
ImportError: attempted relative import with no known parent package
I have tried using sys.path.append but it doesn't seem to work. I have also tried using just .Desktop or .Code instead of the full name, but this gives the same error. I researched packages online, but am struggling to fully comprehend what they are, much less make/use them. Any help would be greatly appreciated.

Turtle Module in python not importing

this is my fist time using the turtle module in python but i can't seem to import it?
Here's my code:
from turtle import *
pen1 = Pen()
pen2 = Pen()
pen1.screen.bgcolour("#2928A7")
and here is the error I get:
Traceback (most recent call last):
File "C:\Python34\Python saves\turtle.py", line 2, in <module>
from turtle import *
File "C:\Python34\Python saves\turtle.py", line 5, in <module>
pen1 = Pen()
NameError: name 'Pen' is not defined
Can anyone tell me what I did wrong?
The problem is that you've named your program "turtle.py".
So when Python sees the statement
from turtle import *
the first matching module named turtle that it finds is your program, "turtle.py".
In other words, your program is basically importing itself and not the turtle graphics module.
Here's some code to demonstrate this problem.
turtle.py
#! /usr/bin/env python
''' Mock Turtle
Demonstrate what happens when you give your program the same name
as a module you want to import.
See http://stackoverflow.com/q/32180949/4014959
Written by PM 2Ring 2015.08.24
'''
import turtle
foo = 42
print(turtle.foo)
help(turtle)
I guess I should show what that code actually prints...
When run as turtle.py it prints the following "help" info:
Help on module turtle:
NAME
turtle - Mock Turtle
FILE
/mnt/sda4/PM2Ring/Documents/python/turtle.py
DESCRIPTION
Demonstrate what happens when you give your program the same name
as a module you want to import.
See http://stackoverflow.com/q/32180949/4014959
Written by PM 2Ring 2015.08.24
DATA
foo = 42
(END)
When you hit Q to get out of the Help, the Help info is displayed again. When you hit Q for the second time, then
42
42
is printed.
Why are the "help" message and 42 printed twice? That's because all the code in turtle.py is executed when it's imported, and then again when its encountered after the import statement. Note that Python doesn't try to import modules that it has already imported (unless explicitly told to do so with reload). If Python did re-import, then the above code would get stuck in an infinite loop of importing.
When run as mockturtle.py it prints:
Traceback (most recent call last):
File "./mock_turtle.py", line 16, in <module>
print(turtle.foo)
AttributeError: 'module' object has no attribute 'foo'
And of course that's because the standard turtle module doesn't actually have a foo attribute.
I think the solution is to type this :
pen1 = turtle.Pen()

Categories

Resources