0

Импорт класса

Есть файл с названием cl, вот его код: class Creature: def __init__ (self, name, age, hp, dmg): self.name = name self.age = age self.hp = hp self.dmg = dmg Есть второй файл, вот его код: from cl import Creature lox = Creature("David", 20, 100, 35 ) print (lox.name, lox.dmg) Что не так с импортом? Пишет "Нет такого модуля"

30th Oct 2020, 6:53 PM
Endrocefalit
Endrocefalit - avatar
8 Réponses
+ 1
Что там за CL? Могу я узнать? What is CL there? May I know?
30th Oct 2020, 7:14 PM
Jayakrishna 🇮🇳
+ 1
Make an empty file called __init__.py in the same directory as the files. That will signify to Python that it's "ok to import from this directory". Then just do... from user import User from dir import Dir P.S. this is not my answer. this was copied from stackoverflow, and it took 2minutes to figure out what is the matter.
30th Oct 2020, 7:37 PM
Shadoff
Shadoff - avatar
0
cl - имя второго файла с которого я хочу импортирвать в будущем классы
30th Oct 2020, 7:16 PM
Endrocefalit
Endrocefalit - avatar
0
если вы запускаете программу здесь, это не сработает. SoloLearn не может получить доступ к вашему локальному хранилищу. Если вы работаете на своем ПК, убедитесь, что вы назвали файл cl.py и сохранены в текущем каталоге, в противном случае укажите полный путь. if you are running Program here, it won't work. SoloLearn can't access your local storage. If you running in your PC, then make sure you named file cl.py and saved in current directory otherwise provide full path.
30th Oct 2020, 7:25 PM
Jayakrishna 🇮🇳
0
Это на pydroid, файлы в одном месте лежат
30th Oct 2020, 7:27 PM
Endrocefalit
Endrocefalit - avatar
0
Can you explain this pls?
30th Oct 2020, 7:29 PM
Endrocefalit
Endrocefalit - avatar
0
Okay, that doesn't work. But i still did it. Answer: I jut must save "cl" as "cl.py"
30th Oct 2020, 7:43 PM
Endrocefalit
Endrocefalit - avatar
0
Save it in correct directory as : cl.py class Creature: def __init__ (self, name, age, hp, dmg): self.name = name self.age = age self.hp = hp self.dmg = dmg Another file do: from cl import Creature lox = Creature("David", 20, 100, 35 ) print (lox.name, lox.dmg)
30th Oct 2020, 8:20 PM
Jayakrishna 🇮🇳