0

So I've been having this issue fairly consistently over different virtual environments I've created. This is my most recent instance. I created a Python 3.6 virtual environment for a project that uses OpenCV. I downloaded the relevant modules to my virtual environment, but whenever I try to import them in my code, I get the following error:

>>> import cv2 as cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type

I've looked up this error before, and from my understanding, it has something to do with the directories Python is looking at when it is trying to import my modules. I checked sys.path and I got the following:

>>> import sys
>>> sys.path
['', '/home/[username]/catkin_ws/devel/lib/python2.7/dist-packages', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/[username]/ECE432_Proj/lib/python3.6/site-packages', '/home/[username]/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

So I'm guessing that the bolded parts are where the error is coming from. When I'm trying to import cv2, it finds cv2.so in a different folder, and tries to import that instead, with no success.

My question is this: how do I modify the sys.path so that Python looks at the correct folders when it is trying to find the right module to import?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Does this answer your question? [adding directory to sys.path /PYTHONPATH](https://stackoverflow.com/questions/16114391/adding-directory-to-sys-path-pythonpath) – Bill Huang Nov 23 '20 at 20:07
  • 1
    Did you define `PYTHONPATH` in your environment? Right now, your Python 3.6 install is looking for libraries under 2.7-specific directories. It wouldn't do that on its own, which makes me think you used `PYTHONPATH` incorrectly (read: at all; it's a hack that's bites you constantly when you need to support multiple versions of Python). `echo $PYTHONPATH` in your regular shell (not Python); I'll guess you made it something terrible (possibly in your `.bashrc`). You *could* remove the bad entries from `sys.path` (it's a plain `list`, with `list` methods), but it's best not to have those entries. – ShadowRanger Nov 23 '20 at 20:07
  • You’re using 2.7 packages in a 3.6 environment ... – donkopotamus Nov 23 '20 at 20:09
  • If you are running this directly though command line you may want to activate it to avoid adding unneeded values to the PYTHONPATH – joshmeranda Nov 23 '20 at 20:09
  • @ShadowRanger, your response put me in the right direction. I did check `PYTHONPATH` in my regular shell, and it was set to the wrong directories. I checked `.bashrc` as well, which I remember changing for a different project, so that explains why `PYTHONPATH` is looking at those directories only. I spent a couple days figuring out how `PYTHONPATH` and `sys.path` work, and I commented out the lines in `.bashrc` for the moment, and have settled for appending the relevant directories to `sys.path`, but I wonder if there is a better way to do that. – CompleteRegistration Nov 26 '20 at 08:23

0 Answers0