Home > OS >  Can't import Python module even though it is in the PYTHONPATH
Can't import Python module even though it is in the PYTHONPATH

Time:10-11

I'm trying to run Pixar's USDZ converter tool but i can't seem to run the Python script, it complains with the error Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH.

I am doing this on Ubuntu instead of the native MacOS, but i have compiled from source the USD library, located in /usr/local/USD/.

Runnig the script outputs the above error:

$ ./usdzconvert -h
  Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH

even though I have added the path to the PYTHONPATH:

$ echo $PYTHONPATH
:/usr/local/USD/lib/python:/usr/local/USD/lib/python/pxr:/usr/local/USD/lib

furthermore Python seems to see that just fine:

$ python3 -c 'import sys; print(sys.path)'
['', '/home/julien', '/usr/local/USD/lib/python', '/usr/local/USD/lib/python/pxr', '/usr/local/USD/lib', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

I've also tried adding print(sys.path) to the begining of the script to confirm it was loading the same env variable and running it i get the same path as above.

The script portion that throws the error is as follows:

#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile

usdLibLoaded = True
kConvertErrorReturnValue = 2

if sys.version_info.major != 3:
    print('  \033[93mWarning: It is recommended to use Python 3. Current version is '   str(sys.version_info.major)   '.\033[0m')

try:
    from pxr import *
    import usdUtils
except ImportError:
    print('  \033[91mError: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH\033[0m')
    usdLibLoaded = False

I don't know anything about python so I am at loss to figure out this import issue, any ideas?

I have python 2 and 3 installed:

$ python --version
Python 2.7.17

python3 --version
Python 3.6.9

but considering the script starts off with a shebang instructing to use python3 i would think this would be fine, no?

CodePudding user response:

I don't known why, but sometimes, something like this worked for me:

PYTHONPATH=. ./usdzconvert -h

How about this:

#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile

usdLibLoaded = True
kConvertErrorReturnValue = 2

if sys.version_info.major != 3:
    print('  \033[93mWarning: It is recommended to use Python 3. Current version is '   str(sys.version_info.major)   '.\033[0m')

pxr_path = '/usr/local/USD/lib/python/pxr'
sys.path.append(pxr_path)
sys.path.append(os.path.dirpath(pxr_path))

try:
    from pxr import *
    import usdUtils
except ImportError:
    print('  \033[91mError: failed to import pxr module. Please check the path to USD\033[0m')
    usdLibLoaded = False

CodePudding user response:

So I've finally figured out what the problem was. I've modified the python script to print the actual import message and this is what I got:

dynamic module does not define module export function (PyInit__tf)

A quick google search brought me to this answer that explains that the module was compiled with python 2, so I recompiled USD with python 3 and re-installed all its corresponding v3 dependencies and then I ran the script again and this time no more import error.

  • Related