Unittest fails to import module
I know the answer to this must be trivial but I am stuck... I am starting on a not too complex Python project. Right now the project file structure contains three subdirectories and two files with Python code: code blablabla.py test blablabla_test.py doc (empty for now) blablabla_test.py contains import unittest and import blablabla $PYTHONPATH points at both the code and the test directories. When I run blablabla_test.py it fails to import blablabla.py I have messed around for oven an hour and get nowhere. I have done unittesting like this with success in the past and I have revisited one of those projects and it still works there. The older project has a slightly flatter structure as it lacks a separate code subdirectory: something.py test something_test.py I have temporarily tried this on the new project but to no avail. Any leads? TIA /Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Unittest fails to import module
In article b38pvbfjlv...@mid.individual.net, Martin Schöön martin.sch...@gmail.com wrote: I know the answer to this must be trivial but I am stuck... I am starting on a not too complex Python project. Right now the project file structure contains three subdirectories and two files with Python code: code blablabla.py test blablabla_test.py doc (empty for now) blablabla_test.py contains import unittest and import blablabla $PYTHONPATH points at both the code and the test directories. A couple of generic debugging suggestions. First, are you SURE the path is set to what you think? In your unit test, do: import sys print sys.path and make sure it's what you expect it to be. When I run blablabla_test.py it fails to import blablabla.py Get unittest out of the picture. Run an interactive python and type import blablabla at it. What happens? One trick I like is to strace (aka truss, dtrace, etc on various operating systems) the python process and watch all the open() system calls. See what paths it attempts to open when searching for blablabla. Sometimes that gives you insight into what's going wrong. I have messed around for oven an hour and get nowhere. What temperature was the oven set at? -- http://mail.python.org/mailman/listinfo/python-list
Re: Unittest fails to import module
On Sat, 29 Jun 2013 19:13:47 +, Martin Schöön wrote: $PYTHONPATH points at both the code and the test directories. When I run blablabla_test.py it fails to import blablabla.py What error message do you get? I have messed around for oven an hour and get nowhere. I have done unittesting like this with success in the past and I have revisited one of those projects and it still works there. [...] Any leads? The first step is to confirm that your path is setup correctly. At the very top of blablabla_test, put this code: import os, sys print(os.getenv('PYTHONPATH')) print(sys.path) What do they say? What should they say? The second step is to confirm that you can import the blablabla.py module. From the command line, cd into the code directory and start up a Python interactive session, then run import blablabla and see what it does. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Unittest fails to import module
On 2013-06-29, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Sat, 29 Jun 2013 19:13:47 +, Martin Schöön wrote: $PYTHONPATH points at both the code and the test directories. When I run blablabla_test.py it fails to import blablabla.py What error message do you get? I have messed around for oven an hour and get nowhere. I have done unittesting like this with success in the past and I have revisited one of those projects and it still works there. [...] Any leads? The first step is to confirm that your path is setup correctly. At the very top of blablabla_test, put this code: import os, sys print(os.getenv('PYTHONPATH')) print(sys.path) Yes, right, I had not managed to make my change to PYTHONPATH stick. I said the explanation would be trivial, didn't I? Thanks for the quick replies. I am back in business now. No, neither English nor Python are native languages of mine but I enjoy (ab)using both :-) /Martin -- http://mail.python.org/mailman/listinfo/python-list