Unittest fails to import module

  • Thread starter Martin Schöön
  • Start date
M

Martin Schöön

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
 
R

Roy Smith

Martin Schöön said:
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?
 
S

Steven D'Aprano

$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.
 
M

Martin Schöön

$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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top