Reputation: 589
I have my application organised with this structure:
main/
config/
__init__.py
# some source .py files
date/
__init__.py
# some source .py files
# some other sources/packages
main.py
__init__.py
bin/
app
**The names above are only examples.
The app
file is also a Python file that inside if __name__ == "__main__":
calls the main function of the main.py
file.
This application is working well, it works from everywhere, running the app
file. We also have uploaded the package on PyPI and there are no problems.
Now I'm trying to write some regression tests (another Python sources) that calls the main application. These tests, what basically do is:
With subprocess
module and its function check_output
, I'm running the app
file, so I'm running something similar to
subprocess.check_output('..\bin\app')
The problem appears when I run my regression tests, then I get an ImportError: No module named
error. I've tried some different things but I'm not able to run my regression tests..
The modules not found are those ones that are inside my application, for example inside the config
directory. I've tried refactoring some things, but always there's something failing with the same error. However, as my application works well when I run it normally, I think that the error is another one, something strange..
Anyone have an idea?
Thanks!
Upvotes: 1
Views: 4128
Reputation: 16007
You haven't pasted the full error message. There's shouldn't be anything stopping you from importing subprocess
. Maybe you misspelled it:
>>> import subproces
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named subproces
>>> import subprocess
>>>
You've got another issue with your code for subprocess.check_output
. Assuming there's nothing else wrong in your code or folder structure that affect the import, change
subprocess.check_output('..\bin\app')
to
subprocess.check_output('..\\bin\\app')
In Python and in many other languages, \
is an escape character. Just as \n
is the escape sequence for a newline, \a
is 'beep' and \b
is 'backspace'.
To see that in action:
>>> print 'hello\bworld' # notice the missing letter in the output
hellworld
>>> print '\a' # you'd hear a beep
>>>
So your string '..\bin\app'
gets interpreted as '..<backspace>in<bell>pp'
:
>>> print '..\bin\app'
.inpp
and you'd hear a beep. So it's unlikely that you'd have a file with that name, and with a beep in its name*.
In order to prevent the backslash (\
) from behaving like an escape character, you have to escape that too with a backslash before it: '\\'
>>> print '..\\bin\\app'
..\bin\app
* (some filesystems theoretically allow that)
Upvotes: 1