Home > Software engineering >  Importing files in python project
Importing files in python project

Time:07-30

I've skimmed over similar questions with importing problems, but I can't figure out what I'm doing wrong. This is my project structure:

root_dir
└── src
    ├── cyclegan
    │   ├── models
    │   │   └── cycle_gan.py
    │   └── utils.py
    └── train.py

I'm trying to run the train.py file like this:

python3 src/train.py

from the root directory of the project. But I have this error which I can't figure out:

Traceback (most recent call last):
  File "src/train.py", line 14, in <module>
    from cyclegan.models.cycle_gan import CycleGAN
  File "/home/ANT.AMAZON.COM/stefler/Documents/university/style-transfer/StyleTransfer/src/cyclegan/models/cycle_gan.py", line 7, in <module>
    from src.cyclegan.utils import *
ModuleNotFoundError: No module named 'src'

How should I import/run correctly?

CodePudding user response:

Remove the parent directory name when you're importing from a sibling package:

root_dir
└── src
    ├── cyclegan
    │   ├── models
    │   │   └── cycle_gan.py
    │   └── utils.py
    └── train.py

inside train.py:

from cyclegan.utils import *
  • Related