1

I have the following layout:

/project
    /thomas
        /users
            /src
                code_alpha.py
            /tests
                code_beta.py

I tried:

/project
    /thomas
        /users
            __init__.py
            /src
                code_alpha.py
            /tests
                code_beta.py

with from users.src import code_alpha

also tried:

 /project
        /thomas
            /users
                __init__.py
                /src
                    code_alpha.py
                    __init__.py

                /tests
                    code_beta.py

with from users.src import code_alpha

I tried to solve the problem with this guide and some similiar topics here, but could not figure out. Adding the directory to my path did not solve the problem.

edit: updated layout.

Samantha
  • 383
  • 1
  • 2
  • 12

2 Answers2

0

Try adding __init__.py to somefolder_3.

Then in code_beta.py you will be able to write

from somefolder_3.somefolder_4 import code_alpha

Abgan
  • 3,696
  • 22
  • 31
  • Do i have to delete the other inits? And just keep the init in somefolder_3 ? I tried both and does not work :( – Samantha Apr 06 '14 at 12:53
0

Did you just run the python script by running the command: python code_beta.py in the folder tests? If you did, you can create an __init__.py in the tests and try to run: python -m users.test.code_beta in the thomas floder (make sure that you have __init__.py in the floder users, test, src).

zhujs
  • 543
  • 2
  • 12
  • Yes. Error message: "Unable to import 'users.src'" – Samantha Apr 06 '14 at 13:22
  • I did this experiment several weeks ago, hope I can help you – zhujs Apr 06 '14 at 13:32
  • I tried like you said. I get the error: "/usr/bin/python: No module named users.tests" Im surprised that python imports from other folders are a real pain in the ass...I have spent hours to solve this...but no result yet. – Samantha Apr 06 '14 at 13:33
  • Wrong spelling? tests or test ? – zhujs Apr 06 '14 at 13:47
  • its tests - But i tried both. I did an error in the thread topic instead of "test" its "tests". But nvm that was not the problem :( – Samantha Apr 06 '14 at 13:49
  • but I try this, it works. Did you run the command using thomas floder as the current wording directory? users.tests must corresponding to you folder name and note that __ is two underscore characters. – zhujs Apr 06 '14 at 13:53
  • Well, it works now. Thank you very much. I have run the command from the wrong folder. – Samantha Apr 06 '14 at 13:58
  • I am very glad that I can help you ^^! – zhujs Apr 06 '14 at 14:02
  • One other thing. I can import it from the thomas folder, but what if I want import it in my code-file test-beta-py? – Samantha Apr 06 '14 at 14:11
  • I can not find a way to do that. I guess that python will not try to search the ancestor folder of your working directory. So if you run `python -m code_beta` in the tests folder, it reports no module named users.src – zhujs Apr 06 '14 at 14:31
  • Hm, tomorrow I can ask some python programmer. Maybe he knows a solution. I will respond again. – Samantha Apr 06 '14 at 14:35
  • Well, there is no really a nice or good solution so far. You have to use something like this http://stackoverflow.com/questions/714063/python-importing-modules-from-parent-folder . But unfortunately it did not work for me. I had to rerange my folders to solve my problem. – Samantha Apr 10 '14 at 09:32