简体   繁体   English

ModuleNotFoundError:在 Visual Studio 代码中没有我的自定义类型名为“xxx”的模块

[英]ModuleNotFoundError: No module named 'xxx' with my custom type in Visual Studio code

I try to run the code below by clicking the red box Run Python File in the screenshot below:我尝试通过单击下面屏幕截图中的红色框Run Python File来运行下面的代码:

car_client.py car_client.py

from MyLib.car import Car

car = Car()
print(car.get_name())

But I get the error below:但我收到以下错误:

 from MyLib.car import Car

ModuleNotFoundError: No module named 'MyLib' ModuleNotFoundError:没有名为“MyLib”的模块

car.py汽车.py

class Car:
   def get_name(self):
      return 'BMW'

在此处输入图像描述

Add the following to the top of the code in the car_client.py file:car_client.py文件的代码顶部添加以下内容:

import sys
sys.path.append("./MyLib")

and modify the import as并将导入修改为

from car import Car

the code will work fine代码可以正常工作

在此处输入图像描述

To make it a module, you need to define an __init__.py file.要使其成为一个模块,您需要定义一个__init__.py文件。

Documentation: https://docs.python.org/3/tutorial/modules.html#packages文档: https://docs.python.org/3/tutorial/modules.html#packages

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModuleNotFoundError:Visual Studio Code 中没有名为“pandas”的模块 - ModuleNotFoundError: No module named 'pandas' in Visual Studio Code ModuleNotFoundError:Visual Studio Code 中没有名为“pyperclip”的模块 - ModuleNotFoundError: No module named 'pyperclip' in Visual Studio Code moduleNotFoundError:没有在 Visual Studio 代码中命名的模块 - moduleNotFoundError: No module named in visual studio code Visual Studio Code ModuleNotFoundError:没有名为“_overlapped”的模块 - Visual Studio Code ModuleNotFoundError: No module named '_overlapped' ModuleNotFoundError: 没有名为 XXX 的模块 - ModuleNotFoundError: No module named XXX ModuleNotFoundError:使用 Visual Studio Code 在 Python 中没有名为“PySide6”的模块 - ModuleNotFoundError: No module named 'PySide6' in Python using Visual Studio Code ModuleNotFoundError:Visual Studio Code 中没有名为“bs4”的模块 - ModuleNotFoundError: No module named 'bs4' just in Visual Studio Code ModuleNotFoundError:没有名为“selenium”的模块。 如何从 Visual Studio Code 修复它? - ModuleNotFoundError: No module named 'selenium'. How to fix it from Visual Studio Code? ModuleNotFoundError:Visual Studio 代码中没有名为“cx_Oracle”的模块 - ModuleNotFoundError: No module named 'cx_Oracle' in Visual Studio Code ModuleNotFoundError:没有名为“pandas”的模块(在 Visual Studio 中) - ModuleNotFoundError: No module named 'pandas' (in visual studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM