24

I installed anaconda by running the

bash Anaconda-2.2.0-Linux-x86_64.sh

command on my Ubuntu 14.04 system , which installed successfully, after which I was asked to export my new /home/username/anaconda/bin $PATH environment variable.

On doing so, I was able to use all of anaconda's features including the IDE's as well as use all conda based commands successfully.

The next time I booted up my system, every miss-typed command saw a

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

error. (All commands except python to be specific)

On following a few stackexchange and askubuntu posts and also noticing that my $PYTHONPATH had been set to usr/local/lib/python2.7, I tried to

export PYTHONPATH=$PYTHONPATH:/home/username/anaconda/lib/python2.7

but it didn't help.

This had me go through a entire saga of package removals and reinstalls, and of course, a lot of updates and upgrades, to try and fix the problem by myself.

conda info -a returns:

CIO_TEST: <not set>
CONDA_DEFAULT_ENV: <not set>
CONDA_ENVS_PATH: <not set>
LD_LIBRARY_PATH: <not set>
PATH: /home/username/anaconda/bin:/home/username/Scala-sbt/sbt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/username/bin:/usr/local/java/jdk1.8.0_20/bin
PYTHONHOME: <not set>
PYTHONPATH: /usr/local/lib/python2.7:/home/username/anaconda/bin/python

The command

which python

returns

/home/username/anaconda/bin/python

and

echo "$PATH"

returns

/home/username/anaconda/bin:/home/username/Scala-sbt/sbt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/username/bin:/usr/local/java/jdk1.8.0_20/bin

I know it's something to do with the way I set the path variables, specifically in the ~/.bashrc in which Anaconda automatically prepended my /home/username/anaconda/bin folder to the $PATH variable (This happened during a second installation of Anaconda after I removed it first).

I haven't modified any other environment variable in either ~/.profile or ~/.bashrc.


I added the export $PYTHONPATH line to my ~/.bashrc before restarting.

All of Anaconda's features work now, although the same Fatal Python error: Py_Initialize: Unable to get the locale encoding error keeps showing up instead of the usual unknown command error, for most mistyped commands.

I will keep looking into this and edit my answer (or refer to existing answers, if any) as soon as I find out why this happens.

7 Answers 7

13

I would recommend unsetting PYTHONPATH. It is generally not needed, and it causes things to break like this by making one Python load things from another Python (in this case, it looks like the system's Python 3 is trying to load something that was written for Python 2).

1
  • 3
    Sincere apologies for the late reply, sir. By unsetting the PYTHONPATH, do you mean manually setting it up on startup everytime? Anaconda runs Python 2.7.10 currently and I haven't installed Python 3, so why would this error show up? The reason why I'm asking is that Conda's info for user site dirs specifies the PYTHONPATH variable as PYTHONPATH: /home/usrnme/anaconda/lib/python2.7:/usr/local/lib/python2.7. If I am to remove the PYTHONPATH: /home/usrnme/anaconda.. line from my ~/.bashrc , the error would still persist, and also none of Anaconda's features would work, until I set it again.
    – samirzach
    Jul 9, 2015 at 11:10
4

I have been having similar issues in the past couple days, so I traced it back to how bash handles "command not found". In Ubuntu 14.04 (and Linux Mint 17, which I uses the 14.04 scripts), /etc/bash.bashrc has the following function:

if [ -x /usr/lib/command-not-found ]; then
    function command_not_found_handle {
        # check because c-n-f could've been removed in the meantime
        if [ -x /usr/lib/command-not-found ]; then
            /usr/bin/python /usr/lib/command-not-found -- $1
            return $?
        else
           return 127
        fi
    }
fi

However, /usr/lib/command-not-found has been rewritten for Python 3. It handles the /etc/bash.bashrc command with:

if sys.version < '3':                                                       
    # We might end up being executed with Python 2 due to an old            
    # /etc/bash.bashrc.                                                     
    import os                                                               
    if "COMMAND_NOT_FOUND_FORCE_PYTHON2" not in os.environ:                 
        os.execvp("python3", [sys.argv[0]] + sys.argv)

This calls "python3" from the path rather than giving the direct path. To correct this, line 22 of /usr/lib/command-not-found should be changed from

os.execvp("python3", [sys.argv[0]] + sys.argv)

to

os.execv("/usr/bin/python3", [sys.argv[0]] + sys.argv)

This appears to be a bug with Ubuntu rather than Anaconda. I will check to see if it appears in later distributions.

3

My problem was a bit different: As one user, I could run python, but as another user, not (I got the same error as OP). Finally, I found out that the permissions and ownership of /usr/lib/python3.5 were screwed. The reason for this was that I had recursively set the permissions and ownership on virtualenv, which ended up in modifying the symlink targets (targetin /usr/lib/python3.5) as well.

Tip: Use strace python to figure out what's going on during Python startup. When I used strace, I could clearly see PERMISSION_DENIED on /usr/lib/python3.5.

2

After having installed python3 in the standard locations and realizing I needed sudo to use it, I installed locally using this in my home directory:

python3 -m venv env_py3
source env_py3/bin/activate

But had more errors. Simply unsetting PYTHONPATH on AWS's Amazon Linux instance worked just great for me.

0

This worked for me

https://stackoverflow.com/a/39097003/823282

rm -rf venv
virtualenv -p /usr/bin/python3 venv/
source venv/bin/activate
pip install -r requirements.txt
0

Yet another answer: I lazily copied my entire ~/.virtualenvs directory from an old machine, and that old machine had 3.6 installed, so when my python3 environments were all created with some command like mkvirtualenv --python=$( which python3 ), they got 3.6. Now, Ubuntu has moved on and the default python3 installation gives 3.8. So, I did

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.6

and I can workon tfgpu; ipython without a crash.

Some other details:

  • I do not have the $PYTHONHOME variable set.
  • My $PYTHONPATH includes just a personal directory (that's currently empty).
  • My ~/.bashrc sources /usr/local/bin/virtualenvwrapper_lazy.sh.
  • I seem to be able to activate environments without it, but workon complains about "check that virtualenwrapper has been installed" etc. if I don't set VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8 (selected because, when I did the new virtualenvwrapper install with sudo pip3 install virtualenvwrapper, 3.8 was the only system 3.x installed on my computer).
-2

I had a similar issue on windows - I deleted the PYTHONHOME system variable. I will try to translate the solution into English. My Computer > Properties > Advanced System Settings > Environment Variables, look for the variable PYTHONHOME and delete it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .