0

I have searched related questions and tested many solutions there, for example, I found [a solution here]:https://askubuntu.com/a/470989/608783, but I found it does not work in my case...

I have the following file structure:

Folder1
  Folder2
     __init__.py
     model.py
  Folder3
     __init__.py
     test.py

In model.py I defined a class:class mymodel(), then in test.py I wanna to use mymodel, so I tried from ..Folder2 import mymodel, from ..Folder2.model import mymodel, but all failed with error ValueError: Attempted relative import in non-package.

What should I do? Thanks for any advice.

Edit: I am very sorry for not list out _init_.py files, they are indeed included in the Folder2 and Folder3. However, the content of them are different from which proposed by @anekix's solution. The following are from my possibly wrong setting:

  • __init__.py in Folder2: from .model import mymodel
  • __init__.py in Folder3: empty

What is wrong with my two __init__.py, thank you again.

Update: One way to solve this problem is by set all __init__.py to empty and then follow @jeffy proposed solution. Another way is by @anekix proposed solution, which should also works for this kind of problem(you should have a try) although not in my case, possibly because the whole project of mine is too messy.

Community
  • 1
  • 1
ytutow
  • 285
  • 1
  • 4
  • 13
  • 1
    As stated in the accepted answer you link to, *you also need a __init__.py in all your folders.* – Thierry Lathuille Mar 24 '17 at 11:28
  • Possible duplicate of [Importing files from different folder in Python](http://stackoverflow.com/questions/4383571/importing-files-from-different-folder-in-python) – maiky_forrester Mar 24 '17 at 11:41
  • your `__init__.py` file seems correct.and in which folder are you making imports .main.py(assuming here you are making imports) file should be in `folder3` – anekix Mar 24 '17 at 12:47

2 Answers2

3

This could work:

import sys
sys.path.append("/path/to/Folder1")
from Folder2.model import classname
Jelly bean
  • 70
  • 2
  • 6
1

you should make both subfolders as a package(by creating __init__.py in both subfolders) for proper management and imports:

structure should be:

Folder1
  Folder2
     __init__.py
     model.py
  Folder3
     __init__.py
     test.py

inside folder2/__init__.py add this code below:

from model import *
# it imports everything defined in model.py so that
# you can access classes or functions from this python file

inside folder3/__init__.py folder3 add this code below:

from test import *
# it imports everything defined in test.py 

Then from inside folder3 you can use the import as(assuming you have some file main.py in folder3) :

from folder2 import someClass 
# someClass is a class defined in `model.py`

So steps are:

make `folder2` a package.
make `folder3` a package.
import using `folder1.someClass` or `folder2.someClass`.
anekix
  • 2,393
  • 2
  • 30
  • 57
  • Running test.py gives: ImportError: No module named Folder2 – Nasef Khan Mar 24 '17 at 11:56
  • why do you import folder2 in test.py or why do you run `test.py`? the code that you want to run should be in `main.py` or watever name you want(inside `folder3`). then use imports from that `main.py` it will help you structure your code in a better way – anekix Mar 24 '17 at 12:13
  • Using your method, I cannot import anything in Folder2 from a script in Folder3. I've tried both "from Folder2 import SomeClass" and "import Folder2.SomeClass". Have you tried running this yourself? – Nasef Khan Mar 24 '17 at 12:28
  • Thank you @anekix for sincere help. But it gives an error: `ImportError: No module named Folder2`. – ytutow Mar 24 '17 at 12:31
  • @ytutow Did you create `__init__.py` in `folder2`?can you paste your modified code structure and contenets inside file maybe some sample class so that i can find out why is it giving error for you – anekix Mar 24 '17 at 12:33
  • Ok, you can make this method work if you add the full path to `Folder1` to `sys.path` as per Jelly bean's answer. Then do `from Folder2 import SomeClass`. – Nasef Khan Mar 24 '17 at 12:37
  • @anekix, yes, I followed your proposed solution and current file structure are listed in edited post. – ytutow Mar 24 '17 at 12:38
  • @ytutow what did you write in `__init__.py` file?make sure its `double underscore` . you should use `__init__.py` not `_init_.py` – anekix Mar 24 '17 at 12:40
  • @anekix, in `_init_.py` in Folder2, it is `from model import *`, while for the other folder it is `from test import *`, i guess i followed your instructions. – ytutow Mar 24 '17 at 12:42
  • @ytutow it should be `__init__.py` with double undescores what you have written is `_init_.py` just single underscore and that is why you are getting error – anekix Mar 24 '17 at 12:43
  • yes, although here I made several typo regrading this (I am sorry), their name are correctly named as `__init__.py`. I mean they are indeed correctly named locally although here I made this typo mistake. – ytutow Mar 24 '17 at 12:44