简体   繁体   中英

PyDev PYTHONPATH does not work for separate test und src directories

This is probably a noob problem. For that I apologize, but I couldn't find a solution so far. I short, for some reason that I don't understand, I can't access modules from my src directory in my tests. My project setup looks like this:

src/package/module.py
tests/package/module_test.py

and my test roughly looks like this:

import package
import unittest
class module_test(TestCase):
   def testSomeMethod(self):
       m = package.SomeClass() #there is class of that name in module.py

I checked the run configuration setup in PyDev and it says that both src and tests are on the PYTHONPATH when I execute the tests. But when I try to run this test, I get the error 'module' object has no attribute 'SomeClass' .

What am I doing wrong?

When you do import package , you import the package, not the module inside it. If you want to import the module, you need to do from package import module (and then refer to the class as module.SomeClass , not package.SomeClass ).

Packages are containers for groups of modules. They don't magically allow you to access everything inside any of the modules (although you can have them automatically import their modules). You still have to import the individual modules in the package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM