I am using Ubuntu 18.04.03 and recently i have installed anaconda on my system. Currently,
which python --> /home/user/anaconda3/bin/python #good
python --version --> Python 3.7.4
which python2 --> /usr/bin/python2 #good
python2 --version --> Python 2.7.15+
which are fine.
But,
which python3 --> /home/user/anaconda3/bin/python3 #oh no
python3 --version --> Python 3.7.4 #oh no again
#python3.6.8 has been installed in /usr/bin/python3.6
#python3 needs to point /usr/bin/python3.6
which is not fine. Also
which python3.7 --> /home/user/anaconda3/bin/python3.7 #please no
python3.7 --version --> Python 3.7.4
#python3.7 has been installed in /usr/bin/python3.7
#python3.7 needs to point /usr/bin/python3.7 not anaconda's 3.7
is not fine.
As you probably understood that when i write python (some version), i want to call interpreter in /usr/bin not anaconda's except when i write only python, i want to call anaconda's interpreter. How can i achieve that?
Check the content of your $PATH environment variable with echo $PATH. Find the anaconda bin directory in the output and change $PATH in your shell profile script (/etc/bash_profile, .bashrc, or .bash_profile for bash) to put it after /usr/bin. You will probably need to create symbolic links using ln -s in order to keep using anaconda's python and python2.
You could try changing the alias pointed by python3.7
echo 'alias python3.7="/usr/bin/python3.7"' >> ~/.bashrc
Then
source ~/.bashrc
Related
I'm using an Intel MacBook Pro and have problems with python and pyenv.
Can anybody explain how this can happen and how I can correct it?
[~] pyenv versions
system
* 3.10.1 (set by /Users/....../.pyenv/version)
[~] python -V
Python 2.7.16
[~] pyenv --version
pyenv 2.2.3
[~]
In my .zshrc file I have
# set correct python version using pyenv
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
In my opinion python -V should return 3.10.1 and not 2.7.16 which seems to be the system version.
I installed pyenv using brew and run MacOS BigSur 11.6.2.
I was able to follow Tim Roberts' suggestion to create a symlink in order to solve this problem on my own Mac. I am writing this answer to help clear up any ambiguity you might have.
The commands I used were:
cd /usr/local/bin
ln -s /usr/local/bin/python3 python
python -V
Which gives me:
[16:57:25] Richards-MBP:bin richardkyu$ python -V
Python 3.8.12
Typing in python to the command line opens up the 3.8.12 interpreter. Also, my python3 is installed by brew I believe.
Following up on the comment, I should clarify that I created a new symlink for python rather than rewriting the one for python3. When I execute
ls -l grep python3
in the /usr/local/bin directory, I get the following output, which I assume is similar to yours:
I am not sure of the exact issue with these symlinks you're seeing, but if you can clarify what you're worried about I would be glad to check it out.
I installed a Django package on GCP (Debian 9 OS), that comes with the following softwares:
Django (2.2.9)
Git (2.25.0)
Apache (2.4.41)
lego (3.3.0)
MySQL (5.7.29)
Node.js (10.18.1)
OpenSSL (1.1.1d)
PostgreSQL (11.6)
Python (3.7.6)
SQLite (3.31.0.)
Subversion (1.13.0)
When I type the command python -V
I get the following python version: 2.17.13
When I type python3 -V
I get the following version: 3.7.6
How can I uninstall the previous version permanently and keep the current one as the default?
Here's what I tried and I didn't work:
$ ls /usr/bin/python*
usr/bin/python /usr/bin/python2.7 /usr/bin/python3.5 /usr/bin/python3m
/usr/bin/python2 /usr/bin/python3 /usr/bin/python3.5m
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
Returns nothing
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
Returns nothing
I would not recommend uninstalling python2. Install python3 and make sure the directory where you install it is included in your systems $PATH variable. You will need to do something like this. Note, depending on how you install python3, some installers will automatically update your $PATH variable.
Example installing python3 in "/Library/Frameworks/python3" directory.
Show current $PATH variable.
echo $PATH
update .bashrc in home directory (append to current $PATH)
PATH="/Library/Frameworks/python3:${PATH}"
reload .bashrc (or exit and start terminal back up)
. ~/.bashrc
Confirm correct python install will be run when typing python3 into terminal.
$which python3
/Library/Frameworks/python3
I have just installed Python 3.5.1 on my Mac (running the latest version of OSX). My system came with Python 2.7 installed. When I type IDLE at the Terminal prompt my system pulls up the original Python 2.7 rather than the newly installed Python 3.5. How do I get my system to default to Python 3.5.1 when I open the IDLE window from Terminal?
Since Python 2 and 3 can happily coexist on the same system, you can easily switch between them by specifying in your commands when you want to use Python 3.
So for Idle, you need to type idle3 in the terminal in order to use it with Python 3 and idle for using it with Python 2.
Similarly, if you need to run a script or reach a python prompt from the terminal you should type python3 when you want to use Python 3 and python when you want to use Python 2.
It's good practice to have your MacOS Python environment set up properly from the beginning making sure that Homebrew installations take precedence over stock MacOS binaries. You want it in usr/local/bin not MacOS default usr/bin.
.bash_profile
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc
Can also create aliases for both.
alias py2='python2.7'
alias py3='python3.6'
Source the file to ensure it takes effect for the current session
source ~/.bash_profile
Homebrew install and setup etc...
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update
brew upgrade --all
brew cleanup
Python3 install
brew install python3
Next
pip3 install virtualenv
Next
pip3 install virtualenvwrapper
When all is finished python3, pip3, virtualenv, and virtualenvwrapper.sh will all be in usr/local/bin.
Result
Every time I install anything or use commands like mkvirtualenv Python 3 is used by default.
You can use the python3 command (instead of using python), or you can simply uninstall the 2.7 version if you don't use it
If you dont have any python 2 scripts that you use, you can delete python2. But its not a problem to have them both installed. You just have to use another path python3 to launch IDLE.
I would prefer to let them both installled so if you have any scripts that are in python 2 you can still run them or you have to port them to python3.
You can switch to any python version in your project by creating a virtual environment.
virtualenv -p /usr/bin/python2.x (or python 3.x)
In case you just want to run a program in a specific version just open shell and enter python2.x or python3.x
Do right thing, do thing right!
Open your terminal,
input python -V, It likely shows:Python 2.7.10
input python3 -V, It likely shows:Python 3.7.2
input where python or which python, It likely shows:/usr/bin/python
input where python3 or which python3, It likely shows:
/usr/local/bin/python3
add the following line at the bottom of your PATH environment variable file in ~/.profile file or ~/.bash_profile under Bash or ~/.zshrc under zsh.
alias python='/usr/local/bin/python3'
OR
alias python=python3
input source ~/.bash_profile under Bash or source ~/.zshrc under zsh.
Quit the terminal.
Open your terminal, and input python -V, It likely shows:
Python 3.7.2
Note, the ~/.bash_profile under zsh is not that ~/.bash_profile.
The PATH environment variable under zsh instead ~/.profile (or ~/.bash_file) via ~/.zshrc.
Hope this helped you all!
By typing python, you are actually referring to a link.
You will find its location with $ which python. In my case it was /usr/local/bin/python. go there $open /usr/local/bin/ and just delete the original python, python-config and idle as they are
identical to the 2.7 files in the same folder.
Then duplicate the 3.5 files and rename them to what you just deleted.
This also changes the default link other editors like Sublime_ReplPython use and updates it therefore to the 3.5 Version. This was my major concern with the standard installation.
I just started setting up a centos server today and noticed that the default version of python on centos is set to 2.6.6. I want to use python 2.7 instead. I googled around and found that 2.6.6 is used by system tools such as YUM so I should not tamper with it. Then I opened up a terminal on my mac and found that I had python 2.6.8 and 2.7.5 and 3.3.3 installed. Sorry for the long story. In short I just want to know how to lookup all the version of python installed on centos so I don't accidentally install it twice.
The more easy way its by executing the next command:
ls -ls /usr/bin/python*
Output look like this:
/usr/bin/python /usr/bin/python2.7 /usr/bin/pythonw
/usr/bin/python-config /usr/bin/python2.7-config /usr/bin/pythonw2.7
we can directly use this to see all the pythons installed both by current user and the root by the following:
whereis python
Find out which version of Python is installed by issuing the command
python --version:
$ python --version
Python 2.7.10
If you see something like this, Python 2.7 is your default version. You can also see if you have Python 3 installed:
$ python3 --version
Python 3.7.2
If you also want to know the path where it is installed, you can issue the command "which" with python and python3:
$ which python
/usr/bin/python
$ which python3
/usr/local/bin/python3
Here is a cleaner way to show them (technically without symbolic links). This includes python2 and python3 installs:
ls -1 /usr/bin/python* | grep '.*[2-3]\(.[0-9]\+\)\?$'
Where grep filters the output of ls that that has that numeric pattern at the end ($).
Or using find:
find /usr/bin/python* ! -type l
Which shows all the different (!) of symbolic link type (-type l).
Use, yum list installed command to find the packages you installed.
COMMAND: python --version && python3 --version
OUTPUT:
Python 2.7.10
Python 3.7.1
ALIAS COMMAND: pyver
OUTPUT:
Python 2.7.10
Python 3.7.1
You can make an alias like "pyver" in your .bashrc file or else using a text accelerator like AText maybe.
As someone mentioned in a comment, you can use which python if it is supported by CentOS. Another command that could work is whereis python. In the event neither of these work, you can start the Python interpreter, and it will show you the version, or you could look in /usr/bin for the Python files (python, python3 etc).
It depends on your default version of python setup. You can query by Python Version:
python3 --version //to check which version of python3 is installed on your computer
python2 --version // to check which version of python2 is installed on your computer
python --version // it shows your default Python installed version.
compgen -c python | grep -P '^python\d'
This lists some other python things too, But hey, You can identify all python versions among them.
Sift through the output of this script.
sudo find / -name 'python*' -type f -exec du -h {} + | sort -r -h ~/Documents/python_locations.txt
ls -l /usr/bin/python* & ls -l /usr/local/bin/python*
I would add to #nurealam siddiq answer,
python --version // it shows your default Python installed version.
python2 --version // to check which version of python2 is installed
python3 --version //to check which version of python3 is installed
python3.X --version // to further check which python3.X is installed
To check python versions installed in your OS you can run the below commands:-
python2 -version
python3 -version
Is there a way to force conda to use the system version of python (along with all of the system libraries) in a given env?
I have conda enabled by default in my shell, which can get a bit annoying, because if I try to run a system python app, it gets a different version of python to what it is expecting (python still defaults to 2.7 on *buntu), and often won't run. I would like the root env of conda to just be a redirect to the system python install.
You need to edit all user shell run commands such as your .bashrc file to prepend the bin directory of anaconda to path export PATH=~/anaconda/bin:$PATH, while in your root run commands append export PATH=$PATH:~/anaconda/bin. In both cases you will have access to the conda command. You can check which python will be run by typing $env python --version. You can also check which other versions would be available and their order of priority (if the other is removed) by using $type -a python. Of course ensure your executable python files have #!/usr/bin/env python and not some other direct route to a python executable. For further info Google BASH Shell look up queries like http://www.cyberciti.biz/tips/an-example-how-shell-understand-which-program-to-run-part-ii.html.
Simply removing the python symlink from ~/miniconda3/bin/ appears to do the job.
$ which python
/home/naught101/miniconda3/bin/python
$ rm /home/naught101/miniconda3/bin/python
$ which python
/usr/bin/python
$ source activate science
discarding /home/naught101/miniconda3/bin from PATH
prepending /home/naught101/miniconda3/envs/science/bin to PATH
(science)$ which python
/home/naught101/miniconda3/envs/science/bin/python
(science)$ source deactivate
discarding /home/naught101/miniconda3/envs/science/bin from PATH
$ which python
/usr/bin/python
So far, this doesn't seem to have caused me any problems. Unfortunately the same doesn't work for ~/miniconda/bin/python3, because conda requires it when switching to other envs that use the same python version. However, that one hasn't caused as many problems in the first place.
If this does cause problems, it's easy enough to undo, just cd ~/miniconda/bin/; ln -s python3 python (or what ever version of python you're using in your conda root env). You may need to activate/deactivate an env to get that version of python back on your PATH.
If you are in (base) or another environment, use conda deactivate, this will exit conda's environment and place you back into the OS environment:
on MacOS
(base) user$ python --version
Python 3.9.12
(base) user$ conda deactivate
user$ python --version
Python 2.7.16
on Linux (with no base python installed)
(base) user$ python --version
Python 3.9.15
(base) user$ conda deactivate
user$ python --version
-bash: python: command not found