skip to Main Content

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

2

Answers


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

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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search