[英]ModuleNotFoundError: No module named 'A given module in package' when running the code
Here is the entire code:这是完整的代码:
MyApp/
* mypkg/
* __init__.py
* mod1.py
* mod2.py
* Demo.py
I am trying to run this app from Demo.py我正在尝试从 Demo.py 运行这个应用程序
mod1.py
: mod1.py
:
class Person:
def __init__(self,name,age,address):
self.name = name
self.age = age
self.address = address
def show_person(self):
return "the name of the person is {0} and age is {1} ".format(self.name,self.age)
mod2.py
: mod2.py
:
import mod1 as P
class Student(P.Person):
def __init__(self,name,age,address,standard,grade):
super(Student,self).__init__(name,age,address)
self.standard = standard
self.grade = grade
def show_student(self):
return "The name of the student is {0} and he is studing in {1} standard".format(self.name,self.standard)
__init__.py
: __init__.py
:
__all__ = ["mod1","mod2"]
Demo.py
: Demo.py
:
from mypkg import *
a = Person('John',12,'Near S School')
print(a.show_person())
b = Student('name', 17, 'Near X Resturant',12, 'B')
print(b.show_student())
The output: output:
Traceback (most recent call last):
File "d:\ROOT\WORK\Python\MyApp\Demo.py", line 1, in <module>
from mypkg import *
File "d:\ROOT\WORK\Python\MyApp\mypkg\mod2.py", line 1, in <module>
import mod1 as P
ModuleNotFoundError: No module named 'mod1'
The output should be a string with name and standard output 应该是一个带有名称和标准的字符串
Example:例子:
The name of the Student is Jhon and he is studying in standard 10.学生的名字叫 Jhon,他正在学习标准 10。
In mod2.py
, change the import to在mod2.py
中,将导入更改为
import mypkg.mod1 as P
In Demo.py
, change the import to在Demo.py
中,将导入更改为
from mypkg.mod1 import Person
from mypkg.mod2 import Student
This should allow you to run the program.这应该允许您运行该程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.