kadina
kadina

Reputation: 5372

why updating sys.path is not effecting the list of directories python will search

I am trying to import serial module. But it was throwing error

AttributeError: 'module' object has no attribute 'RawIOBase'

Later I found that there is one more directory with name 'io' and 'init.py' file exists in the directory. so when I print using print(io), it is displaying

<module 'io' from '/projects/phx/tools/io/__init__.pyc'>

instead of

<module 'io' from '/usr/lib/python2.6/io.pyc'>

To update PYTHONPATH, I used below line in my program

sys.path.insert(0, "/usr/lib64/python2.6")

After this, I am importing serial as below.

exec("import serial")

but it didn't solve the problem. I am getting the same error.

If I add "/usr/lib64/python2.6" to PYTHONPATH in bashrc file, it is working fine.

Can any one please help me to understand why sys.path is not effecting the list of directories python will search?

Upvotes: 1

Views: 443

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59681

FYI, you can join your system path together using

>>> ':'.join(sys.path)
'/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/pymodules/python2.7'

But you shouldn't be setting the env['PYTHONPATH'] as you were doing.

All you need to do is edit your system path like this:

sys.path.insert(0, "/usr/lib64/python2.6")

Any imports after this line will look at /usr/lib64/python2.6 first.

import sys
sys.path.insert(0, "/usr/lib64/python2.6")
import serial

Upvotes: 1

Related Questions