0

I'm in a bit of a python death spiral, here, with the "ImportError: No module named encodings" error when I try using python. I've read a dozen or so different posts on the problem, and none of them help. Advice welcome (please be specific, since I'm a bit of a novice).

The original install was done as part of an OpenCV installation here. I was able to use the virtual environment and do everything I needed to do. Then this morning I picked up the RPi again and did an update/upgrade. Now, when I log on (or source .profile), I get:

pi@raspberrypi:~ $ source .profile
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x76f06010 (most recent call first):
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.

I can run python2.7, but attempting to import locale give a similar error:

pi@raspberrypi:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/locale.py", line 13, in <module>
    import encodings
ImportError: No module named encodings
>>> 

python3 won't run at all:

pi@raspberrypi:~ $ python3
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x76f91010 (most recent call first):
Aborted

Some threads suggest setting $PYTHONHOME and $PYTHONPATH; others say not to (that's the point of the virtual environment). I've tried setting them -- though I'm not 100% sure of what to set them to -- but the outputs above are with them unset. I have set:

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

and the path is:

pi@raspberrypi:~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

This thread suggests rebuilding the virtual environment, but that just gives me the same errors:

pi@raspberrypi:~ $ rm -rf venv
pi@raspberrypi:~ $ virtualenv -p /use/bin/python3/ venv/
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

Current thread 0x76fd3010 (most recent call first):
Aborted

Suggestions for what to try next? I'm hoping it's just an env setting that I've screwed up, but I can't figure it out.

It took a long time to get OpenCV up and running -- I'd hate to have to redo all of that.

  • Isn't opencv a standard module? It should be in the reposotories so why did you spend a long time installing? I'm afraid it seems your installation is broken. It's safest and possibly quickest to re-install. – joan Aug 21 '18 at 7:58
  • All of the tutorials I found linked back to the pyimpagesearch.com site -- I never tried apt for opencv. Ultimately, I think you're right about it being hopelessly broken -- it'd be nice to know how I screwed it up, though. – gcl8a Aug 21 '18 at 13:06
0

It sounds like your install process was a bit complicated, so perhaps the first thing is to get some knowledge of what you have in your modules collection. Examine what you have in your python*/site-packages directories - these will be found in /usr/lib, /usr/lib64, and possibly /usr/local/lib and or lib64. encodings might be there, but the OS or python might need a crutch to be able to find it, with a file soft link "ln -s ..." for example.

  • The encodings directory under python2.7 was empty, but the one under pypy has the scripts. I made a link to it under the 2.7 directory, and now I can import encodings, but other stuff is broken. I've tried setting $PYTHONHOME to the pypy library, but it's no use. So, I think it's hopeless and I have to go back to the start. It'd be nice to know what I did wrong, though -- this all started after a standard apt-get update/upgrade. – gcl8a Aug 21 '18 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.