1

I use anaconda as a way to handle virtual environments. This means I have multiple version of python installed. I experience that the wrong python version starts when I run python from the shell.

Running

Which python
/anaconda3/envs/dash-two/bin/python
type -a python
 python is /anaconda3/envs/dash-two/bin/python
 python is /usr/bin/python

Inspired by this post I have tried hash -t python and looked at the output of alias

0xSheepdog
  • 2,750
  • Which is the "right" and which is the "wrong" python /anaconda3/envs/dash-two/bin/python or /usr/bin/python? – Philip Couling Feb 14 '19 at 13:15
  • I want /anaconda3/envs/dash-two/bin/python – haugstve Feb 14 '19 at 22:37
  • I solved the problem:

    My .bash_profile script had these two lines: export PATH="$PATH:~/bin" export PATH="/opt/local/bin:/opt/local/sbin:$PATH"

    When calling .bash_profile more than once it would keep adding to the path. The built in terminal in VS Code copies you path and runs the the .bash_profile script so starting it has the same effect as running .bash_profile twice.

    The problem was that all since all lines referred to the old $PATH variable it would just keep growing.

    Adding this export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin at the start solved it.

    – haugstve Feb 14 '19 at 23:25

3 Answers3

1

It seems you want to use virtual environments but have not activated one. To do that (assuming you have the basic venv stuff installed — works better for python3)

$ mkdir pytry
$ python3 -m virtualenv pytry

Now you should have a virtual env directory in pytry. cd into pytry and you should see for example

$ ls
bin  include  lib  local  share
$ 

Now run

source bin/activate

Note run above from the virtual env directory (in our case pytry)

Now you should find that your prompt should have changed from (say) $ to (pytry) $

And which python will tell you your python executable

If you want a different executable then at the time of creation of the venv you need to run instead of

python3 -m virtualenv pytry

do

python3 -m virtualenv -p other_python_executable pytry

In general this will show help

$ python3 -m virtualenv -h
Rusi
  • 489
0

This is controlled in the "PATH" environment variable.

PATH is a list of directories to search in order for the command you've typed. It's a single string seperated by colons. Eg: anything I've placed in my home directory (/home/philip/bin) will be used instead of anything in /usr/bin/ because my PATH is set to:

echo $PATH
/home/philip/bin:/usr/local/bin:/usr/bin:/bin:/usr/games

To temporarily change your path you can set it with "export". Eg:

export PATH=/usr/local/bin:/usr/bin:/bin:/usr/games:/home/philip/bin

To set this permanently you will need to set it in your profile. This can be done by putting a line similar to the one above (with your re-ordered path) into a file in your home directory called .profile. If that doesn't exist, just created it and add the line.

0

It looks like Anaconda is stomping on your PATH (i.e. promoting the path of its own python interpreter in front of the path to the default system interpreter). There are a couple of ways to deal with this:

  1. Create the Anaconda instance with the correct version of python:

    conda create -n myenv python=3.4
    
  2. Override the PATH environment variable once you've loaded the anaconda instance.

    export PATH=/usr/bin:$PATH
    
  3. Or place the preferred path in the first line of the script file, set the the file as executable and run it as a shell script. The first line of the script would look something like this:

    #!/usr/bin/python
    

    You would set the file executable with the 'chmod' command:

    > chmod 700 file.py
    

    Then run from the command line as:

    > file.py