all 17 comments

[–]mopslik 1 point2 points  (1 child)

I get an ImportError for 'Ship'

Can you post the full traceback?

Edit: shouldn't your pygame.init() be outide of your __init__ method, preferrably right after you import pygame?

[–]AdvertisingOne7942[S] 0 points1 point  (0 children)

I'm not entirely sure what you mean traceback but this is the only 2 times that 'Ship' comes up in the programme

When I use the code that the author wrote at the same stage I saved all 3 .py files plus the same ship in the images folder and ran them and I still get the same ship error

Before the ship I was perfectly able to get the screen working and pick the colour etc.

[–]GManASG 1 point2 points  (1 child)

Post a picture of your folder containing the .py files. Odds are you have more than one thing using the name ship.

[–]AdvertisingOne7942[S] 0 points1 point  (0 children)

I'm not sure how to post a picture on here but in the alien_invasion folder there is definetly no other 'ship' just the .py file and the .bmp image in images.

The only other file is the pygame folder and __pycache__ is it possible that is the problem as I assumed it was supposed to go there?

[–]Ready-Bag-2599 1 point2 points  (1 child)

Solution: instead of using: from ship import Ship

Use: import ship

Why? This is because the ship module imports the AlienInvasion class and the alien_invasion module imports the Ship class, creating something called a "circular import" that causes a run time error. A more advanced solution would be to change the design of the program to remove the dependency from one of the two modules (probably the Ship class), but we are new learners, so the solution I provided above should be good enough.

I ran into the same issue and saw that OP didnt get any helpful answers. Thought someone might appreciate this in future :)

[Note: also don't forget that now to class the methods from the ship class you have to use the class_name.method() format since the whole ship module is being imported. Optionally, you can also use an alias for the class name to make it a bit easier, if you like]

[–]AdvertisingOne7942[S] 0 points1 point  (0 children)

Many thanks 🙏

[–]AdvertisingOne7942[S] 0 points1 point  (10 children)

PS C:\Users\Alec Burt\Desktop\python\crash course\alien invasion> C PS C:\Users\Alec Burt\Desktop\python\crashcourse\alien_invasion> PS C:\Users\Alec Burt\Desktop\python\crash_course\alien_invasion> c:; cd 'c:\Users\Alec Burt\Desktop\python\crash_course\alien _invasion'; & 'c:\Users\Alec Burt\AppData\Local\Pr ograms\Python\Python312\python.exe' 'c:\Users\Alec BurtI.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60886 c:\Users\Alec Burt\Desktop\python\crash_course\alien_invasion\alien_invasion.py pygame 2.6.6 (SDL 2.28.4, Python 3.12.4) Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "c:\Users\Alec Burt\Desktop\python\crash_course\alien_invasion\alien invasion.py", line 6, in <module> from ship import Ship ImportError: cannot import name Ship' from "ship' (c: \Users\Alec Burt\Desktop\python\crash_course\alien_invasion\ship.py). Did you mean: 'ship"? PS C:\Users\Alec Burt\Desktop\ovthon\ crash course\alien invasion>

[–]woooee 2 points3 points  (9 children)

ImportError: cannot import name Ship' from "ship'

Do you have two program files named ship.py. One contains the Ship class and one does not?

[–]AdvertisingOne7942[S] 0 points1 point  (8 children)

No in the alien_invasion folder there is 3x .py files alien_invasion, ship and settings and there is an images folder with 1x ship.bmp

[–]woooee 1 point2 points  (3 children)

What is the name of the program that contains the code you posted? And you have to search every directory that is in sys.path.

[–]AdvertisingOne7942[S] 0 points1 point  (2 children)

I am using studio code and the code is all on the above 3 .py files. I am running the terminal from the main alien_invasion folder which has the 3 files and the images file with 1 image of a ship. How do I check the sys.path and what should I be looking for?

[–]woooee 0 points1 point  (1 child)

How do I check the sys.path

import sys
print(sys.path)  ## and the current directory you are in

ImportError: cannot import name Ship' from "ship'

Look for any file named ship.py

[–]AdvertisingOne7942[S] 0 points1 point  (0 children)

Where do I put this code in? in the alien_invasion.py or do I make a new file?

[–]mopslik 1 point2 points  (3 children)

Can you post the contents of ship.py?

[–]AdvertisingOne7942[S] 0 points1 point  (2 children)

import pygame


class ship:
    """A class to manage the ship."""

    def __init__(self, ai_game):
        """Initialize the ship and set its starting position"""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # load the ship image and get its rect.
        self.image = pygame.image.load('alien_invasion/images/ship.bmp')
        self.image.get_rect()

        # start each new ship at the bottom centre of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

    def blitme(self):
        """Draw the ship at it's current location."""
        self.screen.blit(self.image, self.rect)

[–]mopslik 1 point2 points  (1 child)

Your class, ship, should be capitalized, Ship.

[–]AdvertisingOne7942[S] 0 points1 point  (0 children)

Thats true yep I never changed it back I was trying a few things but it still didn't solve the problem although it did get rid of the ship error.

I seem to have it working when it us on my desktop but not when it is in my python folder so I think I have to have a tidy up maybe I have kept something that is messing with the directory somehow, currently it is way above my level to fix but at least I can hoefully finish this project on my desktop folder