Why do I get an import error on this?

S

Steve

I'm trying to run a Python program on Unix and I'm encountering some
behavior I don't understand. I'm a Unix newbie, and I'm wondering if
someone can help.

I have a simple program:
____________________________________________

#! /home/fergs/python/bin/python
import sys, os
import cx_Oracle
____________________________________________

If I run it through the Python interpreter, this way:

it runs fine.

But if I try to run it as an executable script, this way:

I get an import error that says it can't find cx_Oracle.

Why does it behave differently when I run it in these two ways, and
what do I need to do to make it run successfully either way?
 
R

Ron Adam

Steve said:
I'm trying to run a Python program on Unix and I'm encountering some
behavior I don't understand. I'm a Unix newbie, and I'm wondering if
someone can help.

I have a simple program:
____________________________________________

#! /home/fergs/python/bin/python
import sys, os
import cx_Oracle
____________________________________________

If I run it through the Python interpreter, this way:


it runs fine.

But if I try to run it as an executable script, this way:


I get an import error that says it can't find cx_Oracle.

Why does it behave differently when I run it in these two ways, and
what do I need to do to make it run successfully either way?

I ran across something similar with using py2exe. I think it occurs
when there are more than one file with the same name in different
locations in the search path. Try renaming cx_Oracle to _cx_Oracle then
import as...

import _cx_Oracle as cx_Oracle

Of course your problem might be entirely different. But this might help.

Cheers,
Ron
 
F

Fredrik Lundh

Steve said:
I'm trying to run a Python program on Unix and I'm encountering some
behavior I don't understand. I'm a Unix newbie, and I'm wondering if
someone can help.
If I run it through the Python interpreter, this way:


it runs fine.

But if I try to run it as an executable script, this way:


I get an import error that says it can't find cx_Oracle.

Why does it behave differently when I run it in these two ways, and
what do I need to do to make it run successfully either way?

in the first case, the shell uses the PATH variable to search for a python
interpreter. in the second case, the shell uses the path specified in the
script.

what does "which python" say?

</F>
 
M

Micah Elliott

I have a simple program:
____________________________________________

#! /home/fergs/python/bin/python
import sys, os
import cx_Oracle
____________________________________________

If I run it through the Python interpreter, this way:


it runs fine.

But if I try to run it as an executable script, this way:


I get an import error that says it can't find cx_Oracle.

Are "which python" and "/home/fergs/python/bin/python" the same? If
not, it appears that "which python" can see cx_Oracle, but the latter
cannot. You could mess with PYTHONPATH or get /home/.../python into the
front of your PATH.
 
S

Steve

I did "which python" and the two paths were different. Once I fixed
the path in the script, it worked fine.

Thanks Frederik and Micah!
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top