apache2 using python2.7 & I want to use python3.4 - python

I setup my server using python bottle & mod_wsgi
my bottle script are all writing by python3.4 but the apache2 server using by default python2.7.6 ?
is there a way to make the python3.4 the default python on apache2
I'm stock right now

I think you have installed mod_wsgi for python2 with this command:
sudo apt-get install libapache2-mod-wsgi
But if you want to use mod_wsgi with python3, you should install correct mod_wsgi with this command:
sudo apt-get install libapache2-mod-wsgi-py3

To change a python version on per user basis you simply create an alias within user's home directory. Open ~/.bashrc file and add new alias to change your default python executable:
alias python='/usr/bin/python3.4'
Once you make the above change, re-login or source your .bashrc file:
$ . ~/.bashrc
Check your default python version:
$ python --version
Python 3.4.2
Change python version system-wide
To change python version system-wide we can use update-alternatives command. Logged in as a root user, first list all available python alternatives:
# update-alternatives --list python
update-alternatives: error: no alternatives for python
The above error message means that no python alternatives has been recognized by update-alternatives command. For this reason we need to update our alternatives table and include both python2.7 and python3.4:
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
The --install option take multiple arguments from which it will be able to create a symbolic link. The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.4 and as a result the /usr/bin/python3.4 was set as default python version automatically by update-alternatives command.
# python --version
Python 3.4.2
Next, we can again list all python alternatives:
# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.4
From now on, we can anytime switch between the above listed python alternative versions using below command and entering a selection number:
# update-alternatives --config python

Related

How to set default Python version without --update-alternatives

I have Python3.6 and Python3.8 installed on Ubuntu 18.04. When I execute python3 -V I get python3.8.0, which is correct. That's currently my default Python.
I used the Ubuntu 18.04 package repository when I installed Python3.6 and Python3.8. I just built Python3.10 from source and I want to set that as the default now. The canonical way to change is with --update-alternatives, but I did not do that when I installed Python3.6 and Python3.8, so when I execute:
update-alternatives --query python
update-alternatives: error: no alternatives for python
Python3.8 is in /usr/bin/python3.8 and Python3.10 is in /usr/local/bin/python3.10.
According to what I have read on the net, I should do this:
$ sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3.8
$ sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.10
Then I select the default with sudo update-alternatives --config python.
Because I currently don't have any alternatives but I was somehow able to switch from Python3.6 to Python3.8 (I don't remember how), will the commands above cause any problems? Is there a problem in the different locations for 3.8 (/usr/bin/) vs 3.10 (/usr/local/bin/) and what will be the effect of the symlink for 3.10 going into /usr/local/bin/ where it's already located.
Naturally I could just try it, but if there is an issue I'm not sure how to back out of it if it goes wrong. I've gotten a lot of differing information on the net.
Thanks for any help.
I have answered my own question. First, I did have alternatives, but as Ubuntu defaults to Python2 (please Ubuntu change that -- the world has moved on), the command is sudo update-alternatives --config python3:
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.8 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/bin/python3.8 2 manual mode
Note the line "providing /usr/bin/python3." That means the Python symlinks for Python 3 are in /usr/bin/python3. So I add the new one:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 3
That makes Python 3.10 priority 3, the highest priority. Then:
$ python3 --version
Python 3.10.0
I hope that helps someone in the future.

create the python soft link fails

I have multiple Python versions, and thus also include multiple Python binary executables.
ls /usr/bin/python*
shows the python environments in my ubuntu 18.04
/usr/bin/python /usr/bin/python2.7 /usr/bin/python2-pbr /usr/bin/python3 /usr/bin/python3.5m /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/bin/python3m
/usr/bin/python2 /usr/bin/python2-jsonschema /usr/bin/python2-wsdump /usr/bin/python3.5 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3-config /usr/bin/python3m-config
in order to satisfy the PyFlink requirement regarding the Python environment version, i have to choose to soft link python to point to your python3 interpreter:
ln -s /usr/bin/python3 python
however when I use the command, it tells me
ln: failed to create symbolic link 'python': File exists
so i wonder do I need to delete the /usr/bin/python first and then use the command to create the soft link?
Command ln -s /usr/bin/python3 python creates link ./python pointing to /usr/bin/python3. You probably executed it multiple times so ./python already exists. You could overwrite it by providing -f flag to ln.
You definitely should not delete /usr/bin/python.
The whole idea with manually creating links to Python interpreter to install a package seems very weird. I suggest one of the following options:
just use the full path to the interpreter: /usr/bin/python3.6 -m pip install <package>; if the package adds any scripts globally usable from command line, like pyspark, they will be installed with hashbang pointing to the interpreter you installed them with
use a virtual environment: /usr/bin/python3.6 -m venv ~/.env-py36; source ~/.env-py36/bin/activate; python -m pip install <package>
use something like pyenv

How to set Python 3.8 as my default python version on kali linux

I installed python 3.8 in my kali linux OS, but when I type python in kali linux terminal it showing like this
> Python 2.7.18 (default, Apr 20 2020, 20:30:41)
How can I set Python 3.8 as my default?
Hope this answers your question.
The easiest way would be to run alias python=python3 .
So, now if you run the command python it will load python3 instead of python while being under the same alias/command python.
What I usually do is install non-default versions of programs under /usr/local. Then I configure the file $HOME/.bash_profile so that the $PATH variable has /usr/local/bin first, before /usr/bin and /bin (which might actually be the same physical directory on your system).
$HOME/.bash_profile:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# PATH starts with...
PATH="/usr/local/bin"
# add $HOME/bin if it exists...
if [ -d "${HOME}/bin" ]; then
PATH="${PATH}:${HOME}/bin"
fi
# These should exist, add them as fall back....
PATH="${PATH}:/bin:/usr/bin:/sbin:/usr/sbin"
# Some custom locations last
if [ -d /usr/local/arm-elf/bin ]; then
PATH="${PATH}:/usr/local/arm-elf/bin"
fi
if [ -d /opt/local/bin ]; then
PATH="${PATH}:/opt/local/bin"
fi
This has the effect of making anything located in /usr/local/bin the default when I am using an interactive shell, and letting the system defaults remain in effect otherwise.
first, check if you have python3.8 installed
use
ls /usr/bin/python*
then check for an alternative python version using
update-alternatives --list python
if the output of python3.8 is set to 2
To set the default python command to run python3.8
use this to create a persistent symlink
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
the 2 depends on your python alternatives
then
python --version
First remove python 2.7.18
Type >> sudo apt purge -y python2-minimal
install python latest version
Type >> sudo apt install -y python3-pip
latest version of python is installed!

How do I change my default python 3 having more than one version installed (Ubuntu 18.04)? [duplicate]

This question already has answers here:
Installed Python 3.8 on Ubuntu, but it's not the default
(3 answers)
Closed 2 years ago.
When I type python --version in my terminal, it shows Python 3.8.5, but when I type python3 --version, it shows Python 3.6.9. I want to create a virtual environment using python3 -m venv .venv with the version 3.8.5, but because of the fact that my default python3 version is the 3.6.9, it's creating a virtual environment using 3.6.9 as it's version. How do I change my default python3 version?
The python and python3 commands are usually soft links to the actual executables and you can change the targets. For example:
Firstly, find out where python 3.6 and python 3.8 are located:
# which python
/usr/bin/python
# ls -l /usr/bin/python
/usr/bin/python -> python3.8
# which python3
/usr/bin/python3
# ls -l /usr/bin/python3
/usr/bin/python3 -> python3.6
Then, change the soft links:
# rm /usr/bin/python3
# ln -s /usr/bin/python3.8 /usr/bin/python3
Run this Command in your terminal
sudo update-alternatives --config python
You'll get choice prompt, enter selection number of your desired python version.
But if this shows an error like this: update-alternatives: error: no alternatives for python3
Then you have to update your update-alternatives, then you will be able to set your default python version.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6.9
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8.5
Now run below command to set default python
sudo update-alternatives --set python /usr/bin/python3.8

Change default python version in idle (Ubuntu)

So, I'd like to have Python 2.7.6 when I open the idle (that is the one pre-installed on Ubuntu); instead, the idle opens with the one that I have installed (2.7.5).
How can I do that?
Try
sudo update-alternatives --config python
and see the update-alternatives man page. If Ubuntu does not offer the alternatives automatically, you'll have to add them yourself, e.g.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.5 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.5 20

Categories

Resources