简体   繁体   English

ModuleNotFoundError:运行代码时没有名为“包中的给定模块”的模块

[英]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 tomod2.py中,将导入更改为

import mypkg.mod1 as P

In Demo.py , change the import toDemo.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.

相关问题 ModuleNotFoundError:运行代码时没有名为“pandas”的模块 - ModuleNotFoundError: No module named 'pandas' when running code ModuleNotFoundError: 没有名为“的模块”<package> &#39; - ModuleNotFoundError: No module named '<package>' 运行时PyBuilder“ModuleNotFoundError:没有命名模块” - PyBuilder “ModuleNotFoundError: No module named” when running ModuleNotFoundError:运行时没有名为“psycopg2”的模块 - ModuleNotFoundError: No module named 'psycopg2' when running 运行子 package 模块时出现 ModuleNotFoundError - ModuleNotFoundError when running child package module ModuleNotFoundError:ubuntu 上没有名为“包”的模块 - ModuleNotFoundError: No module named 'package' on ubuntu ModuleNotFoundError:没有名为“code.victim”的模块; “代码”不是 package - ModuleNotFoundError: No module named 'code.victim'; 'code' is not a package ModuleNotFoundError:安装 email package 时没有名为“cStringIO”的模块 - ModuleNotFoundError: No module named 'cStringIO' when installing email package ModuleNotFoundError:运行 GCP 数据流作业时没有名为“oracledb”的模块 - ModuleNotFoundError: No module named 'oracledb' when running GCP Dataflow jobs ModuleNotFoundError:运行 celery worker 时没有名为“social.models”的模块 - ModuleNotFoundError: No module named 'social.models' when running celery worker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM