Importing files in python project

Question:

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?

Asked By: woofwoof

||

Answers:

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 *
Answered By: Mohammad Jafari
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.