4

Running Ubuntu 14.04 and updater says that all software is up to date. Have tried rebooting.

I am preparing for a class that uses Python. I have installed it and it works fine. However, I now have this problem: When I type an unknown command into bash, I get a python error:

cliff@Climate:~$ UnknownProgram

Fatal Python error: Py_Initialize: Unable to get the locale encoding
  File "/usr/lib/python2.7/encodings/__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax 
Aborted

cliff@Climate:~$ env | grep -i python 
PYTHONPATH=/usr/lib/pymodules/python2.7:/usr/lib/python2.7

cliff@Climate:~$ which UnknownProgram    
cliff@Climate:~$ which python   
/usr/bin/python

cliff@Climate:~$ python --version  
Python 2.7.6

cliff@Climate:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/loc‌​al/games

I think that pycharm also installed some python3 on the box.

I have found that /usr/lib/command-not-found includes a shebang for python3. I guess that it is somehow picking up the wrong Py_Initialize file. How do I correct this since I need python 2.7 for the class?

Dan
  • 6,784

3 Answers3

2

Removing the export PYTHONPATH from my .bashrc has fixed the problem. I now get the normal processing if I type in an incorrect command name. I will have to do some more research to be sure that my python 2.7 code is picking up the correct modules without that extra environment setting.

0

I confirm.

This happens on system with anaconda working fine, when upgrading from ubuntu 17.4 to 17.10, where system now uses Python3.6. unset PYTHONHOME made it work directly.

So I removed from my .bashrc this addition during anaconda install:

export PATH=/opt/anaconda/bin:$PATH
export PYTHONHOME=/opt/anaconda/

Beside, the default python is still python2, not anaconda distrib, but I can still switch to my python env with python2.7,

$  source activate myenv

or run the /opt/anaconda/python2, so that works fine, it is only that is now python3 This system depend PYTHONHOME is not robust to nominal env changes, which is bad in my opinion, and not documented about anaconda install, although workaround is easy. nge

Katu
  • 3,663
0

When using Python, avoid placing Python-specific configuration into your shell env directly. Instead, specify Python paths, variables, etc inside the Python scripts you write and even then, only when needed.

The reason for this is that it allows each Python script or program to call Python its own way without interfering with or generally inhibiting the functionality of other python scripts and programs. In this particular case, you were specifying a Python 2.7 path for your bash shell to use universally and in doing so, broke any programs that rely on a different Python environment variable ( Python 3 in this case) yet do not specify those variables explicitly.

Specifying which version of Python to use at the beginning of a Python script will implictly set the correct default paths for the program to work, nullifying the need for explicit Python environment variables such as the ones you had set in your .bashrcexport file. You can do this by making the first line in your Python scripts something like this:

#!/usr/bin/python2

Conversely, you can specify Python 3 to be used when running a script with thise line as the first line in your script:

#!/usr/bin/python3

You can take this a step further by specifying exact Python versions if you so desire, I.e. :

#!/usr/bin/python2.7

or

#!/usr/bin/python3.4

MGodby
  • 1,172