python – How to import .py modules from subfolders to folders


I have been searching on the net what is the best way to import modules in Python? and I have found these three:

  • Modifying PYTHONPATH .
  • Adding the directory with sys.append("/ruta_directorio") .
  • With a .pth file.

My structure is the following:

├── carpeta1
│   ├──
│   ├──
│   ├──
│   └── subcarpeta1
│       ├──
│       └──

The issue is I need to import some in . Both carpeta1 and carpeta2 have the file.

Could someone tell me what the procedure is for this?

Update (details)

Both folder1 and folder2 have the file.


If you're in and want to import some just need:

from carpeta1.archivo import foo

Since the main script directory ( ) is in the default PYTHONPATH it is not necessary to modify it. Just import as you would from

Scroll to Top