简体   繁体   中英

ImportError: No module named mymodule.main

I have a test which requires instructions on how to run. The goal beyond working is to be noobproof, the instruction manual should consist of one command to run file, one to run the test. My friend said running unittests won't require files being on pythonpath because it checks current directory first, but I get:

import unittest

from ordoro_test.main import OrdoroETLMachine

class ETLMachineTests(unittest.TestCase):

    def setUp(self):
        self.api_url = 'https://9g9xhayrh5.execute-api.us-west-2.amazonaws.com/test/data'
        self.headers = {'accept': 'application/json'}

    def test_data_is_returned(self):
        print(OrdoroETLMachine.get_email_data())



if __name__ == '__main__':
    unittest.main()

在此处输入图片说明

cchilders:~/ordoro_test [master]$ python test.py 
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    from ordoro_test.main import OrdoroETLMachine
ImportError: No module named ordoro_test.main
cchilders:~/ordoro_test [master]$ ls -l
total 8
-rw-rw-r-- 1 cchilders cchilders    0 Mar  5 19:15 __init__.py
-rwxr-xr-x 1 cchilders cchilders 3099 Mar  5 20:12 main.py
-rwxr-xr-x 1 cchilders cchilders  441 Mar  5 20:19 test.py

How can I fix and allow imports the simplest way possible? Thank you

I try

from ordoro_test.assignment.main import OrdoroETLMachine

在此处输入图片说明

No dice

Adding empty __init__.py file in one level with ordoro_test folder shall fix your problem.

See Python 2.7, Modules section for more information.

Use explicit relative import from .main import OrdoroETLMachine . intra-package-references

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