1

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?

clamentjohn
  • 3,417
  • 2
  • 18
  • 42

2 Answers2

3

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.

  • Hey, quick question: When I add the PYTHONPATH I'm unable to add the other modules (cv2, numpy, etc). It seems the original PATH has been lost. How do I append into the original PATH or how do I fix this now to include the other modules? – clamentjohn Oct 10 '18 at 20:25
  • So this depends on how you installed those libraries. I think if you installed those with pip then they should be added to `/usr/lib/python2.7/`. Check to see if numpy and cv2 are in those directories, in which case change the export to: export PYTHONPATH=$PYTHONPATH:path/to/g2opy:/usr/lib/python2.7 If they weren't installed using pip then you need to find where they have been installed and add those paths to PYTHONPATH. Generally it is not a good idea to manually install packages using build_make. Always try to use pip if you can (although it seems g2opy does not have a pip binding). – KhanageRush Oct 10 '18 at 20:50
  • If you find that nothing is working with PYTHONPATH set, you can always just remove it from .bashrc and open a new terminal window. Another alternative is to just move the g2opy library to that /usr/lib/python2.7 directory (or whichever version of Python you are using), and you should be able to import it then. – KhanageRush Oct 10 '18 at 21:02
2

If you installed the package locally, you need to set PYTHONPATH. Seems like it's not set properly.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62