pythong2o

python import successful in one terminal but fails in another


I downloaded and installed (build + make) a cython package, g2opy successfully. And when I tried checking if everything went well, I get this:

(cv) clmno@machine:~/OpenSource/python/g2opy$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:47) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import g2o
>>> import numpy
>>> import cv2

So, I assume everything is fine and opened another terminal window. And tried importing the same module, but failed:

(cv) clmno@machine:~$ python
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:47) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import g2o
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'g2o'

Has this to do with the shared library (.so file)? If it was successfully imported, why would it fail the next time?


Solution

  • In the second terminal, you are running Python in a different directory compared to the first terminal. This suggests that the library you built is not in the Python path. It worked in the first terminal because the g2o library is in the directory where you are currently running Python. As Matthieu suggested, add "~/OpenSource/python/g2opy" to your PYTHONPATH environment variable.

    In ~/.bashrc, add:

    export PYTHONPATH=$PYTHONPATH:path/to/g2opy
    

    then run source ~/.bashrc to update the environment variable in the current path.