Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Importing files in python project

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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 *
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading