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,753
  • 5
  • 26
  • 43
  • are you sure there's not a file called UnknownProgram? which does not tell you if it exists. – wxl Sep 02 '14 at 19:20
  • Is UnknownProgram the actual name for something? If not, can you provide us with the program you're actually calling? – Mitch Sep 02 '14 at 19:36
  • Why did you install Python? Ubuntu comes with Python preinstalled .(at least it should, since many system scripts, such as the updater are written in Python.) – s3lph Sep 02 '14 at 19:42
  • Yes, I'm using the preinstalled python. The problem happens on any unrecognized name. I just used unknownProgram as an example of something that would not be found on many *nix boxes. It also happens if I type in lillypad or yournamehere. – user145510 Sep 02 '14 at 20:03
  • Whats the output of $PATH – Dan Sep 02 '14 at 20:19
  • echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games – user145510 Sep 02 '14 at 20:25
  • I'm pretty sure that the problem is somehow related to this in my .bashrcexport PYTHONPATH=/usr/lib/pymodules/python2.7:/usr/lib/python2.7 – user145510 Sep 02 '14 at 20:25
  • I have used synaptic to reinstall command-not-found but it still has the shebang for python3. My guess is that the PYTHONPATH is causing python to pick up the wrong initialization file. However, I don't know how to get 2.7 and 3 to coexist. – user145510 Sep 02 '14 at 20:27

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,593
  • 26
  • 42
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,162