ModuleNotFoundError: No module named 'mylib'

OS/X VENTURA 3.7 , PYTHON3 ,
import img2pdf

from PIL import Image

import os

CODE…

ModuleNotFoundError: No module named ‘mylib’

You haven’t provided enough details.

Post the smallest code that produces the problem and its traceback (error messages) in full.

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

#code plus error
import img2pdf
from PIL import Image
import os
pngfilename = “test1.png”
pngfname = fname[:-4]
print(“processing:”, pngfname)
pdffilename = pngfname + (“.pdf”)
image = Image.open(fname)
pdf_bytes = img2pdf.convert(image.filename)
file = open(pngfname, “wb”)
file.write(pdf_bytes)
print(“saving:”, pdffname)
image.close()
file.close()raceback (most recent call last):
File “/Users/johnpendreich/app.py”, line 1, in
import img2pdf
File “/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/img2pdf.py”, line 25, in
from PIL import Image, TiffImagePlugin, GifImagePlugin, ImageCms
File “/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/PIL/Image.py”, line 33, in
import logging
File “/Users/johnpendreich/logging.py”, line 3, in
import mylib
ModuleNotFoundError: No module named ‘mylib’
johnpendreich@Johns-iMac ~ %

You have a file named “logging.py” located in the directory “/Users/johnpendreich/”. This file shadows Python’s logging module, i.e. when PIL tries to import Python’s logging module, it instead imports your logging.py. Your logging.py imports something called “mylib”, which Python cannot find, which is what the error message tells you.

You need to rename your logging.py file to something else.

In general, you should never give a Python script a name which already exists in Python’s standard library. Here is a list.

  1. Using an environment is highly recommended for all Python development, like virtualenv.
  2. Install virtualenv with: pip install virtualenv. It’s about 4MB.
  3. Here’s how to use it.
  4. Make your project dir for example python\myproject. Go into myproject.
  5. Make an environment like this: virtualenv .venv. Your environment is now named .venv.
  6. Activate your environment each time you go into this project dir with .venv\scripts\activate.
  7. Now install your third-party modules in this environment.
  8. Edit your myprog.py program. Just add a few imports and maybe a few lines for testing the imported modules were installed correctly.
  9. Now run your program with python myprog.py.

To go to another project.

  1. Deactivate current environment with .venv\scripts\deactivate.
  2. Change directories to python\myproject2.
  3. Activate new environment assuming it has been initialized with .venv\scripts\activate.
  4. Run your other program with python myprog2.py.

Did the error message really say that “mylib” is missing? Because you didn’t import it. Or it may be part of another module that you didn’t install correctly because you didn’t use an environment.

Tips

  1. Never have a python file you make that is the same name as another python module. It causes all kinds of confusing problems. A person above mentions “logging.py”.

Help us help you. Read this link first and learn how to format code so we can help you better. Do not use a screen shot of your code as some of us will copy and paste the code to get it to work for you, so you can learn from this. About the Python Help category You will get more help this way.