Home > OS >  ModuleNotFoundError: No module named 'xxx' with my custom type in Visual Studio code
ModuleNotFoundError: No module named 'xxx' with my custom type in Visual Studio code

Time:08-10

I try to run the code below by clicking the red box Run Python File in the screenshot below :

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'

car.py

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

enter image description here

CodePudding user response:

Add the following to the top of the code in the car_client.py file:

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

and modify the import as

from car import Car

the code will work fine

enter image description here

CodePudding user response:

To make it a module, you need to define an __init__.py file.

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

  • Related