0

I want to import .py in a subfolder from a .py in another subfolder. I have learned you should add init.py in subfolder and you should include your root path

here is my forlder structure :

pycharm_project_973
    __init__.py
    /slic
        multi_slic.py
        __init__.py
    /muilti
        main_unet_up4.py
        __init__.py

my directory, just to make sure. Is it correct with stupid mistake? here is some import code I tried in main_unet_up4.py :

from pycharm_project_973.slic.multi_slic import  multi_slic as multi_slic
from pycharm_project_973.slic.multi_slic import DataGenerator as DG
import pycharm_project_973

each time and I got :

ModuleNotFoundError: No module named 'pycharm_project_973'

then I tried:

import slic

ModuleNotFoundError: No module named 'slic'

from slic.multi_slic import DataGenerator as DG

ModuleNotFoundError: No module named 'slic'

import slic.multi_slic 

ModuleNotFoundError: No module named 'slic'

I am using anaconda virtual environment and tmux. This folder is a pycharm promote project location in server. I use mark directory as source root in pycharm and it worked well then I tried to work on server it remind me this No module issue.

Please help, thanks!

edit after the comment I tried to use:

 from ..model import multi_net 

I got

ValueError: attempted relative import beyond top-level package

sys.path.append("..")
from model import multi_unet

It worked well. I guess it is because that the root dir has been added to my system lib search list.

Tina Liu
  • 195
  • 12
  • See [this question](https://stackoverflow.com/questions/1054271/how-to-import-a-python-class-that-is-in-a-directory-above) for referencing in a relative path that is above the current directory – Alex W Jun 06 '19 at 16:30
  • @AlexW I solved it, thank you. Now I need to figure out how it work, the whole path and module system . Thank you again. – Tina Liu Jun 07 '19 at 17:19

0 Answers0