If you have asdf installed you can easily download/install new Python interpreters:
# Install Python plugin
for asdf:
asdf plugin - add python
# List all available Python interpreters:
asdf list - all python
# Install the Python interpreters that you need:
asdf install python 3.7 .4
asdf install python 3.6 .9
# etc...
# If you want to define the global version:
asdf global python 3.7 .4
# If you want to define the local(project) version:
#(this creates a file.tool - versions in the current directory.)
asdf local python 3.7 .4
1) Install Required Packages for source compilation
$ sudo apt - get install build - essential checkinstall $ sudo apt - get install libreadline - gplv2 - dev libncursesw5 - dev libssl - dev libsqlite3 - dev tk - dev libgdbm - dev libc6 - dev libbz2 - dev
Extract the downloaded package in place. (replace the 'x's with your downloaded version)
$ sudo tar xzf Python - x.x.x.tgz
Your new Python bin is now located in /usr/local/bin
. You can test the new version:
$ pythonX.X - V Python x.x.x $ which pythonX.X / usr / local / bin / pythonX.X # Pip is now available for this version as well: $ pipX.X - V pip X.X.X from / usr / local / lib / pythonX.X / site - packages(python X.X)
The commands below are for Mac but pretty similar to Linux (see the links below)
#Install pyenv brew update brew install pyenv
Let's say you have python 3.6 as your primary version on your mac:
python--version
Output:
Python <your current version>
Let's take 3.7.3:
pyenv install 3.7 .3
Make sure to run this in the Terminal (add it to ~/.bashrc or ~/.zshrc):
export PYENV_ROOT = "$HOME/.pyenv"
export PATH = "$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
Install any build dependencies. On Debian-based systems, use:
apt update apt install build - essential zlib1g - dev libncurses5 - dev libgdbm - dev libnss3 - dev libssl - dev libsqlite3 - dev libreadline - dev libffi - dev libbz2 - dev
Download and unzip file in /usr/local/src
, replacing X.X.X
below with the python version (i.e. 3.8.2
).
cd / usr / local / src
wget https: //www.python.org/ftp/python/X.X.X/Python-X.X.X.tgz
tar vzxf Python - X.X.X.tgz
Before building and installing, set the CFLAGS
environment variable with C compiler flags necessary (see GNU's make
documentation). This is usually not necessary for general use, but if, for example, you were going to create a uWSGI plugin with this python version, you might want to set the flags, -fPIC
, with the following:
export CFLAGS = '-fPIC'
Build the project with make
and install with make altinstall
to avoid overriding any files when installing multiple versions. See the warning on this page of the python build documentation.
make - j 4 make altinstall
Instructions. Add the PPA
sudo add - apt - repository ppa: deadsnakes / ppa sudo apt update && sudo apt upgrade
Now suppose you want Python 3.5:
sudo apt install python3 .5 python3 .5 - dev python3 .5 - venv
Now if you need to create a venv for a specific project,
cd your - project python3 .5 - m venv.venv source.venv / bin / activate
To install for instance the 3.7 as an addition to the installed Python (shell command line, as root or with sudo
):
dnf install python3 .7
There is no pip
, one must start with (shell command line, regular user):
python3 .7 - m ensurepip--user--altinstall
#--altinstall will create pip3 .7 but will not overwrite pip3
and then we can normally continue:
pip3 .7 install--user pytest...# or whatever you need
Updated date Apr 13, 2022
Open Windows PowerShell as administrator and execute the following command to generate the copies
# Python 2.7: copy C: \Python27\ python.exe C: \Python27\ python27.exe # Python 3.10: copy C: \Python310\ python.exe C: \Python27\ python310.exe
Execute the following command to run your python program using python2
python27
Execute the following command to run your python program using python3
python3
There is another method that can be helpful in handling different python versions, which is to use virtual environments. You can use the following command to create virtual environments specific to the python version.
# Python 2.7: python27 - m venv venv2 #Python 3.10: python310 - m venv venv3
Use the following command to activate the virtual environments
# Python 2.7: venv2\ scripts\ activate # Python 3.10: venv3\ scripts\ activate
The first link on the above page should read Latest Python 3 Release - Python 3.X. On that page, scroll to the "files" section and copy the URL of the Gzipped source tarball.,The latest version of Python can always be found on the Python Source Releases page on Python.org:,You should be hit with a prompt like the one below. This will list all the versions of Python your system recognizes. Select the version of Python you'd like to use by providing the "selection" number to the prompt:,This may take a moment to complete. Once finished, go ahead and verify that the version of Python 3 you wanted is installed on your box:
$ apt update && apt upgrade - y
$ sudo apt - get install build - essential checkinstall $ sudo apt - get install libreadline - gplv2 - dev libncursesw5 - dev libssl - dev\ libsqlite3 - dev tk - dev libgdbm - dev libc6 - dev libbz2 - dev libffi - dev zlib1g - dev
$ cd / opt
$ sudo wget https: //www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
$ sudo tar xzf Python - 3.8 .0.tgz
$ cd Python - 3.8 .0 $ sudo. / configure--enable - optimizations $ sudo make altinstall
$ python3--version Python 3.8 .0
$ update - alternatives--install / usr / bin / python python / usr / bin / python3 .6 1 $ update - alternatives--install / usr / bin / python python / usr / bin / python3 .8 2
If copy_function is given, it must be a callable that will be used to copy each file. It will be called with the source path and the destination path as arguments. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used.,If copy_function is given, it must be a callable that takes two arguments src and dst, and will be used to copy src to dst if os.rename() cannot be used. If the source is a directory, copytree() is called, passing it the copy_function(). The default copy_function is copy2(). Using copy() as the copy_function allows the move to succeed when it is not possible to also copy the metadata, at the expense of not copying any of the metadata.,If the destination is on the current filesystem, then os.rename() is used. Otherwise, src is copied to dst using copy_function and then removed. In case of symlinks, a new symlink pointing to the target of src will be created in or as dst and src will be removed.,If given, extra_args is a sequence of (name, value) pairs that will be used as extra keywords arguments when the archiver callable is used.
>>> shutil.which("python")
'C:\\Python33\\python.EXE'
from shutil
import copytree, ignore_patterns
copytree(source, destination, ignore = ignore_patterns('*.pyc', 'tmp*'))
from shutil
import copytree
import logging
def _logpath(path, names):
logging.info('Working in %s', path)
return [] # nothing will be ignored
copytree(source, destination, ignore = _logpath)
import os, stat
import shutil
def remove_readonly(func, path, _):
"Clear the readonly bit and reattempt the removal"
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(directory, onerror = remove_readonly)
>>> from shutil
import make_archive
>>>
import os >>>
archive_name = os.path.expanduser(os.path.join('~', 'myarchive')) >>>
root_dir = os.path.expanduser(os.path.join('~', '.ssh')) >>>
make_archive(archive_name, 'gztar', root_dir)
'/Users/tarek/myarchive.tar.gz'
$ tar - tzvf / Users / tarek / myarchive.tar.gz drwx-- -- --tarek / staff 0 2010 - 02 - 01 16: 23: 40 . / -rw - r--r--tarek / staff 609 2008 - 06 - 09 13: 26: 54 . / authorized_keys - rwxr - xr - x tarek / staff 65 2008 - 06 - 09 13: 26: 54 . / config - rwx-- -- --tarek / staff 668 2008 - 06 - 09 13: 26: 54 . / id_dsa - rwxr - xr - x tarek / staff 609 2008 - 06 - 09 13: 26: 54 . / id_dsa.pub - rw-- -- -- - tarek / staff 1675 2008 - 06 - 09 13: 26: 54 . / id_rsa - rw - r--r--tarek / staff 397 2008 - 06 - 09 13: 26: 54 . / id_rsa.pub - rw - r--r--tarek / staff 37192 2010 - 02 - 06 18: 23: 10 . / known_hosts