How to change python3 to default in linux mint - python-3.x

I have a little problem with changing python3 to default in newly installed operating system linux Mint 19.3. It was pretty easy on Ubuntu 16.04 but now I need small help here.
So, I run
python --version
and got this
Python 2.7.15+
Than I run
python3 --version
and this was the result
Python 3.6.8
After entering this command
sudo update-alternatives --config python
I received obvious info
update-alternatives: error: no alternatives for python
Both version of python are located in /usr/bin folder.
The issue occurs when I'm trying to change python3 to as a default by typing the command
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6
The terminal outputs the following
update-alternatives: --install needs <link> <name> <path> <priority>
Any help here would be welcome.

In your case, priority is missing, just append 1 at the end of the command like this :
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1

To define python3 as your default version of python on linux, you need to add a specific line to .bashrc file located in your home directory.
Here is a simple method to do so through command line interface (termianl).
Run:
$ echo "alias python='python3'" >> .bashrc
$ source .bashrc
then run python --version to check if the change has applied.

#darkfidis answer is correct. But you may also need to run to set or change the default python version.
sudo update-alternatives --config python

Related

Remove Link Pointing to Old Version of Program in Linux

I installed Python 3 with:
yum install python3 -y
When I do:
which -a python3
I get:
/usr/bin/python3
Which is correct.
However, if I enter:
python3
I get:
-bash: /usr/local/bin/python3: No such file or directory
Apparently, python3 is still linking to a prior version which I have removed. How can I fix this so my system recognizes Python 3 being in /usr/bin/python3 instead of /usr/local/bin/python3.
Since you're dealing with an updated PATH starting a new terminal session should pickup this change.
As stated on the comment section this was the case.
For completeness, if the new path (/usr/bin/python3) is stil not on your PATH on the new session you could manually add it on /etc/profile (globally) or ~/.bash_profile (for your user, assuming you are using bash)

How to get the python3 path in ubuntu without which command

Problem:
I want to get the python3 path in my Ubuntu VM. Like /usr/xyz/python3
More Information:
I dont see the 'which' command present in my envt.
I tried to 'sudo apt install which', but it fails saying that no such package found.
Get path of the executable python binary
Python code
import sys
print(sys.executable)
Command
python -c "import sys; print(sys.executable)"
sudo apt install which is failing because which is part of debianutils; most executables don't come as their own package.
sudo apt install debianutils
If you use bash, you can just do :
type -p python
The where and which commands are often an alias to the inbuilt bash command type.
I have it also as app in busybox on most embedded devices.
Also tablets and smartfons with the app: ConnectBot
If present on your machine too; then: type -p python3
But anyway, the correct/recommended way to make a SHEBANG is not to use type, where or which.
If your PATH environment variable targets the usually folders for installed executables just use: #!/usr/bin/env python3
Example: test.py
#!/usr/bin/env python3
import sys
print('No error - But i return 1 and not 0')
sys.exit(1)
Making than executable with: chmod +x test.py
And execute it...
$ ./test.py
No error - But i return 1 and not 0

installed python 3x on mac but terminal still show version still 2x

I installed python 3x via home brew, the process was successful.
However when I check version, it shows 2x
here is the terminal output
➜ ~ brew install python
Warning: python 3.7.2 is already installed, it's just not linked
You can use `brew link python` to link this version.
➜ ~ brew link python
Linking /usr/local/Cellar/python/3.7.2... Error: Permission denied # dir_s_mkdir - /usr/local/Frameworks
➜ ~ sudo brew link python
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
➜ ~ python -V
Python 2.7.10
I think I need to modify some kind of path. And do some magic then pray...lol
Anyway anyone know how to get this work on my machine? I'm mac latest
First, solve your permission problem by running the official command from the Homebrew Documentation, Troubleshooting page
cd /usr/local && sudo chown -R $(whoami) bin etc include lib sbin share var opt Cellar Caskroom Frameworks
Then run brew link python
And finally run echo $PATH and check that your /usr/local/bin has precedence over other directories.
you could do brew uninstall python3 and install it from the python website here https://www.python.org/
When you go install a module you do pip3 install packageName
And when you run a program you could run it from your IDE (I recommend VSCode) or run it from the terminal with python3 drag_python_file_here
you have to use python3 instead of python on Mac so like python3 path/to/file.py and pip is now pip3 so like pip3 install pillow

-bash: /usr/bin/virtualenvwrapper.sh: No such file or directory

I can't figure out where the shell is trying to run /usr/bin/virtualenvwrapper.sh upon server login. I want virtualenvwrapper permanently uninstalled, not just removed from the shell instance. I thought I uninstalled it with pip uninstall virtualenvwrapper, but every time I log into the server I get the error -bash: /usr/bin/virtualenvwrapper.sh: No such file or directory, as if there is some sort of leftover artifact. Yesterday I did a lot of tinkering and I can't remember all the changes I made or how I made this happen. Where is it executing the search for virtualenvwrapper.sh?
SUPPLEMENTARY INFO
$ echo $PATH
/usr/lib64/qt-3.3/bin
/usr/local/bin/ibm/lsf/9.1/linux2.6-glibc2.3-x86_64/etc
/usr/local/bin/ibm/lsf/9.1/linux2.6-glibc2.3-x86_64/bin
/usr/local/bin
/bin
/usr/bin
/usr/local/sbin
/usr/sbin
/sbin/usr/local/bin/CASAVA-1.8.2/bin
/usr/local/bin/blast
/usr/local/bin/mirdeep2
/usr/local/bin/velvet
$ sudo vim ~/.bashrc
1 # .bashrc
2
3 # Source global definitions
4 if [ -f /etc/bashrc ]; then
5 . /etc/bashrc
6 fi
7
8 # User specific aliases and functions
on ubuntu 12.04 LTS, installing through pip, it is installed to
/usr/local/bin/virtualenvwrapper.sh
And if you are using Ubuntu 16.04 or later, it is installed to
~/.local/bin/virtualenvwrapper.sh
Setting up a Virtual Environment
Now open your terminal in the home directory by right clicking and choosing the option “Open in Terminal”. You can also press the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.
You first need to create a special directory that will hold all of your virtual environments. So proceed with creating a new hidden directory called virtualenv.
$ mkdir .virtualenv
Now you should install pip for Python3.
$ sudo apt install python3-pip
Confirm the pip3 installation.
$ pip3 --version
Now install virtualenv via pip3.
$ pip3 install virtualenv
To find where your virtualenv was installed, type:
$ which virtualenv
Install virtualenvwrapper via pip3:
$ pip3 install virtualenvwrapper
We are going to modify your .bashrc file by adding a row that will adjust every new virtual environment to use Python 3. We will point virtual environments to the directory we created above (.virtualenv) and we will also point to the locations of the virtualenv and virtualenvwrapper.
Now open the .bashrc file using Vim editor.
$ vim .bashrc
If you still haven’t used the Vim editor or you don’t have it installed on your computer you should install it now. It is a widely used Linux editor, and for good reason.
$ sudo apt install vim
After you've installed Vim open the file .bashrc file by typing the vim .bashrc command in your terminal. Navigate to the bottom of the .bashrc file, press the letter i to enter the insert mode of Vim, and add these rows:
#Virtualenvwrapper settings:
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/home/your_username/.local/bin/virtualenv
source ~/.local/bin/virtualenvwrapper.sh
After you are done, press the esc key. Then type :wq and press enter. This command will save and exit the Vim editor. Close and reopen your terminal when you’re done.
To create a virtual environment in Python3 and activate it immediately, use this command in your terminal:
$ mkvirtualenv name_of_your_env
You should confirm that this environment is set up for Python3:
$ Python -V
To deactivate the environment use the deactivate command.
$ deactivate
To list all available virtual environments use the command workon or lsvirtualenv (same result as workon but shown in a fancy way) in your terminal:
$ workon
$ lsvirtualenv
To activate one specific environment use workon + name of your environment:
$ workon name_of_your_env
There are several useful command you might need to use someday:
Rmvirtualenv will remove a specific virtual environment located in your .virtualenv directory.
$ rmvirtualenv name_of_your_env
Cpvirtualenv will copy the existing virtual environment to a new virtual environment and activate it.
$ cpvirtualenv old_virtual_env new_virtual_env
Well done! You have now created your first isolated Python 3 environment.
There are a number of files that might be run when you login to your terminal if you are using the bash shell.
You should check ~/.bashrc, ~/.bash_profile, ~/.bash_login and ~/.profile for "/usr/bin/virtualenvwrapper.sh".
Likely one of those files is being run on login and contains the missing script which you uninstalled.
It can be that you python packages are installed somewhere else. So try:
$ which python
/home/tesla/miniconda3/bin/python
or
$ which virtualenvwrapper.sh
/home/tesla/miniconda3/bin/virtualenvwrapper.sh
To check the location of python installation. In my case I was using miniconda, therefore system was not able to find the location mentioned in the documentation.
If the above location is not /usr/local/bin/virtualenvwrapper.sh
then now use :
source /home/tesla/miniconda3/bin/virtualenvwrapper.sh
Should work.
UPDATE Nov-2022:
I would recommend using pipenv (official docs) for creating and managing virtual environments.
For anyone finding this in the future. The virtualenvwrapper.sh script is/was now located at /usr/share/virtualenvwrapper/virtualenvwrapper.sh on Ubuntu 20.04.1 LTS (at least for me in my VM).
(Sadly i can't just comment on the above post mentioning the locations so it would all be together, because new user reputation)
sudo -H pip3 install virtualenvwrapper
i ran into to similar problem where installation could not suceed because ~/.cache/pip and ~/.cache/pip/http is not owned by the current user. installing with -H or --set-home option solved my problem.
Confirmed for Ubuntu 18 , as already answered by #Tarique . The shell script virtualenvwrapper.sh for the wrapper is within - ~/.local/bin/
(base) dhankar#dhankar-1:~/opencv_cuda$ cd ~/.local/bin/
(base) dhankar#dhankar-1:~/.local/bin$ ls -ltr
total 100
-rwxr-xr-x 1 dhankar dhankar 41703 Jul 23 20:56 virtualenvwrapper.sh
-rwxr-xr-x 1 dhankar dhankar 2210 Jul 23 20:56 virtualenvwrapper_lazy.sh
-rwxr-xr-x 1 dhankar dhankar 259 Jul 23 20:56 virtualenv
I also confronted the same problem, but this worked for me: -
Create a directory to hold the virtual environments.mkdir $HOME/.virtualenvs
Add a line like export WORKON_HOME=$HOME/.virtualenvsto your
.bashrc.
Add a line like source /path/to/this/file/virtualenvwrapper.sh
to your .bashrc.
Run: source ~/.bashrc
Run: mkvirtualenv temp
This time, the "temp" environment is included.
Run: workon temp
The virtual environment is activated.
If you are on Ubuntu 20 then use the code given below in ~/.bashrc
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source ~/.local/bin/virtualenvwrapper.sh
In my case on Ubuntu 20, I found this script in:
/usr/share/virtualenvwrapper/

Yum crashed with Keyboard Interrupt error

I installed the newer version of python (3.2.3) than the one available in Fedora16 (python2.7)
And now yum stops working. It shows the following error.
[root#localhost yum-3.4.3]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
Please advice as how to resolve the error. It would be helpful as I am not able to update or install any package.
Because yum does not support Python3.
You can run command vi /usr/bin/yum, change /usr/bin/python to /usr/bin/python2 in first line.
Thus you can run the command yum by Python2 instead of Python3.
Note however that this will make your setup unsupported and thus unmaintainable (as does what you did). You will likely have other similar problems in the future with other system packages.
If you want to use an alternative Python installation, consider installing it into /usr/local, /opt or using pyenv.
This issue happens when user upgrades to python3, Just simply edit the file --> /usr/bin/yum and change to first line to --> "#!/usr/bin/python2"
The above solution wouldn't solve the all yum dependency problems, its better to run the below commands.
sudo ln -s /usr/local/bin/python3 /usr/bin/python3 (Mark latest python as python3)
sudo ln -sf /usr/bin/python2.7 /usr/bin/python (nake 2.7 as default python)
THanks,
Daman
I'm guessing you installed Python 3.2.3 from source and used "make install" as the last command. That command has the unfortunate side-effect of replacing the system installed version command "python" with the new version. Using "make altinstall" doesn't replace "python".
The command "python" is just a symbolic link to "python2", which in turn is a symbolic link to "python2.7". You should be able to restore "python" by executing the following command:
cd /usr/bin
sudo ln -s python2 python
Thanks Damanvir! Changing the line in /usr/bin/yum worked!
This is a little off topic and might be removed but it might help someone.
These are the steps I used to install Python 3.7 on Centos and fix the yum error.
Download from https://www.python.org/
tar -xvf
./configure --enable-optimizations
make
make install
OR
make altinstall
make altinstall is used to prevent replacing the default python binary file /usr/bin/python.
cd /usr/bin
Remove the current symbolic link to the previous version
rm python
OUTPUT: rm: remove symbolic link ‘python’? y
Find the location of the new version
whereis python3.7
OUTPUT: python3: /usr/local/bin/python3.7
Verify this is correct
/usr/local/bin/python3.7 --version
OUTPUT: Python 3.7.0
Create a symbolic link to the location of the new version
ln -s /usr/local/bin/python3.7 python
python --version
OUTPUT: Python 3.7.0
Yum commands will show the following error:
File "/bin/yum", line 30
except KeyboardInterrupt, e:
SyntaxError: invalid syntax
Change the top line of this file from using python to python2
vi /usr/bin/yum
#!/usr/bin/python2
Reference: https://tecadmin.net/install-python-3-7-on-centos/
The real answer - to ensure that you are back on a supportable version of python (in the event you are looking at this for an issue with RHEL 7).
cd /usr/bin
sudo unlink python
sudo ln -s python2 python
Your yum looks for python2. Let's use 'alternatives' to switch between pythons2 and python3.
run --> sudo alternatives --config python
Enter to keep the current selection[+], or type selection number:
If you don't configure it. How to do that?
sudo alternatives --install /usr/bin/python python
/usr/local/bin/python3.8 60
sudo alternatives --install
/usr/bin/python python /usr/bin/python2 50
It's only a binary link issue. You can copy the working /usr/bin/python2 from any system and copy to current system location /usr/bin/python2
#cd /usr/bin
#ln -s python2 python

Resources