63
The simplest way would be to add an alias to python3
to always point to the native python
installed. Add this line to the .bash_profile
file in your $HOME
directory at the last,
alias python="python3"
Doing so makes the changes to be reflected on every interactive shell opened.
20
As Inian suggested, you should alias python to point to python 3. It is very easy to do, and very easy to switchback, personally i have an alias setup for p2=python2 and p3=python3 as well to save on keystrokes.
Read here for more information: How do I create a Bash alias?
Here is an example of doing so for python:
alias python=python3
Like so:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
See here for the original:
https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3
- [Django]-Requirements.txt greater than equal to and then less than?
- [Django]-How to strip html/javascript from text input in django
- [Django]-Django 1.8 β what's the difference between migrate and makemigrations?
16
pyenv
is a 3rd party version manager which is super commonly used (18k stars, 1.6k forks) and exactly what I looked for when I came to this question.
edit: I use it for several years now. Works like a charm.
Installation
Install pyenv
.
Usage
$ pyenv install --list
Available versions:
2.1.3
[...]
3.8.1
3.9-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
[... a lot more; including anaconda, miniconda, activepython, ironpython, pypy, stackless, ....]
$ pyenv install 3.8.1
Downloading Python-3.8.1.tar.xz...
-> https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
Installing Python-3.8.1...
Installed Python-3.8.1 to /home/moose/.pyenv/versions/3.8.1
$ pyenv versions
* system (set by /home/moose/.pyenv/version)
2.7.16
3.5.7
3.6.9
3.7.4
3.8-dev
$ python --version
Python 2.7.17
$ pip --version
pip 19.3.1 from /home/moose/.local/lib/python3.6/site-packages/pip (python 3.6)
$ mkdir pyenv-experiment && echo "3.8.1" > "pyenv-experiment/.python-version"
$ cd pyenv-experiment
$ python --version
Python 3.8.1
$ pip --version
pip 19.2.3 from /home/moose/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)
- [Django]-Django reverse lookup of foreign keys
- [Django]-Using window functions in an update statement
- [Django]-Django β get() returned more than one topic
13
You can just specify the python version when running a program:
for python 2:
python filename.py
for python 3:
python3 filename.py
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
- [Django]-Checking for empty queryset in Django
- [Django]-How do I reuse HTML snippets in a django view
7
You can open and use zsh terminal on Mac OS.
Edit file /Users/{your_username}/.zshrc
using nano or vim.
Add new alias for python 3
alias python="python3"
Save and check your python version using this following command.
python --version
Look at the result:
- [Django]-How to use curl with Django, csrf tokens and POST requests
- [Django]-Django ignores router when running tests?
- [Django]-Django / Comet (Push): Least of all evils?
4
If you have python various versions of python installed,you can launch any of them using pythonx.x.x
where x.x.x
represents your versions.
- [Django]-Django β get() returned more than one topic
- [Django]-Set up a scheduled job?
- [Django]-Django and Bootstrap: What app is recommended?
1
I have followed the below steps on a MacBook.
- Open the terminal.
- Type nano ~/.bash_profile and enter. (Or vim instead of nano if you use vim.)
- Now add the line alias python=python3
- Press CTRL + x then y to save it. (Or just save it on vim since you canβt exit vim.)
- It will prompt for the file name, simply hit enter.
- Now check the python version by using the command: python βversion
- If you see 2.0.0+, it worked!
- [Django]-In the Django admin interface, is there a way to duplicate an item?
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)
- [Django]-Reference list item by index within Django template?
1
Adding to Inianβs answer (Accepted one),
- To check python 3 version
python3 -V
- To switch to new version of python which is already installed (eg. 3.7 to 3.9)
alias python="python3.9"
-
To install new version of python, you can use homebrew on MAC
-
Once homebrew is installed, you can install new python version with
homebrew
brew install python@3.9
and then switch to this new version using
alias python="python3.9"
-
Check python version to confirm the change
-
To check all the installed versions of python
brew list | grep python
- [Django]-How do I restrict foreign keys choices to related objects only in django
- [Django]-Django Rest Framework: Dynamically return subset of fields
- [Django]-Temporarily disable auto_now / auto_now_add
0
Here is a nice and simple way to do it (but on CENTOS), without braking the operating system.
yum install scl-utils
next
yum install centos-release-scl-rh
And lastly you install the version that you want, lets say python3.5
yum install rh-python35
And lastly:
scl enable rh-python35 bash
Since MAC-OS is a unix operating system, the way to do it it should be quite similar.
- [Django]-Custom django admin templates not working
- [Django]-Django β limiting query results
- [Django]-How can I test binary file uploading with django-rest-framework's test client?
0
I am a beginner in python and was looking for the same and in the terminal, I just typed python3
and it came up with the newest version. I am thinking that if one wants to go to a different version they can just type that in? Could be wrong but this is what shows up when I typed python3.
% python3
Python 3.9.2 (v3.9.2:1a79785e3e, Feb 19 2021, 09:06:10)
[Clang 6.0 (clang-600.0.57)] on darwin
Before when I just typed python. This is the message I would get.
% python2.7
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
- [Django]-Django β Simple custom template tag example
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
- [Django]-How can I obtain the model's name or the content type of a Django object?
0
In order to easily manage the different python versions. Please use below link to see how to use the versions effectively and without any environment variables.
https://youtu.be/jTN4MHNhJZs
- [Django]-How to render menu with one active item with DRY?
- [Django]-WSGI vs uWSGi with Nginx
- [Django]-How to disable Django's CSRF validation?
0
ON WINDOWS HOWEVERβ¦
sometimes you could just rename the file to "python3" in a python 3 enviroment
the program itself will still work but some ides will break for obvious reasonsβ¦
so my answer works on windows but it makes ides that dont have support for enviroments break
why am i the only windows user to mention this
- [Django]-Aggregate() vs annotate() in Django
- [Django]-Error: Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>
- [Django]-How to server HTTP/2 Protocol with django
0
On windows you can use conda
1- install conda Link to install
2- install required py version with conda
conda create --name py=3.8 python=3.8
:: conda create --name <env_name> python=<version>
3- init the terminal that you are using
conda init powershell
:: conda init <something>
4- Now you switch to any version dedfined on step 2 .
python --version
:: output current version for eg python 2.7
conda activate py=3.8
python --version
:: output python 3.8
- [Django]-Can I use a database view as a model in Django?
- [Django]-How to get URL of current page, including parameters, in a template?
- [Django]-ImproperlyConfigured: The included urlconf <project>.urls doesn't have any patterns in it