简体   繁体   中英

Can't import: PyCharm "Unsolved Reference", Python "No module named ..."

A file named Item defines class Fists :

class Fists(Weapon):
    def __init__(self):
        super().__init__(name="Fists",
                         desc="Just fists",
                         value=0,
                         usable=1,
                         damage=1)

A file named Enemy in the same directory defines class Slime :

import Item

class Slime(Enemy):
    def __init__(self):
        super().__init__(enemyhealth=23,
                         enemyweapon=Item.Fists,
                         enemyshield=None)

As you see, I'm trying to give instances of Slime the ability to instantiate Fists , but PyCharm gives the warning: Unsolved Reference 'Item' . When run, Python gives an ImportError No module named 'Item' . How can make it work? I searched everywhere and nothing helped me.

Make sure your IDE is in the current directory (use a shortcut or os.chdir ). If you are using IDLE, in some versions it will not work if you right click and press 'Edit with IDLE'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM