Related:

INSTALL PY3

win

Install py2 first! Install py3 later.
Download and install py3 from the official website.
During installation, make sure to "add to system PATH".
If it is only in user's "Path", we need to also add to system "PATH" (both the python root folder and the script subfolder), and make sure it is before py2's folders.

ubuntu

Ubuntu 16.04 has both py2 & py3; default py2.

INSTALL PIP IN UBUNTU 16

sudo apt-get update && sudo apt-get -y upgrade && \
sudo apt-get install python-pip python3-pip && \
pip -V; pip3 -V

FREQUENT PIP CMD

pip install package_name
pip uninstall package_name
pip search package_name
pip freeze
pip --help

JUPYTER

see: jupyter

PYENV

install

Run curl[ref]:
(manually do it if lacking curl)

curl https://pyenv.run | bash

Update .bashrc:

cat >>~/.bash_profile <<EOD
# pyenv
if [ -d "$HOME/.pyenv" ]; then
  export PYENV_ROOT="$HOME/.pyenv"
  export PATH="$PYENV_ROOT/bin:$PATH"
  eval "$(pyenv init -)"
  eval "$(pyenv virtualenv-init -)"
fi
EOD
source ~/.bash_profile

Install python:

pyenv install 2.7.8

check py version

Tip: to find py version:

# in terminal:
python --version
# in py/jupyter:
import sys
sys.version_info

print(sys.executable) # https://stackoverflow.com/a/46902466
print(sys.version)
print(sys.version_info)

PIPENV

A state-of-the-art combined tool chain.

install

Tip: better to have pyenv installed also, so any python version can be automatically installed by pipenv.

sudo apt install -y python3-pip python3-setuptools && \
python3 -m pip install --user pipenv && \
cat >>~/.bash_profile <<EOD
export PATH="\$HOME/.local/bin:\$PATH"
EOD
source ~/.bash_profile

create env

mkdir sunnyProj; cd sunnyProj
pipenv --python 3.6.7 # will automatically install the specified python in env, if pyenv is installed.

activate env

pipenv shell

how it works

A pip-environment is based on one folder.
Actually, the Pipfile in the folder.
When creating a new environment, it will use the folder's name as the environment's name, and will also search for the environment using the folder's anme when trying to activating by pipenv shell.
When activating one environment, a specific Python's installation path and Python lib path will be added to PATH and have higher priority than system one.
"Pipenv" uses "virtualenvs" as the backend (?).

Ref:
Install and kick-start, (originally from phoikoi.io)

pipenv (bak) (we can use pipenv --python directly as it will install the related python)

list of packages/libraries

Bash:

pip freeze
# or: 
pip list

Python:

import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print(installed_packages_list)

# OR (one-line)
sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])

Ref: stackoverflow

pipenv + jupyter kernels

Ref: stackoverflow

VIRTUAL ENV

OBS: dated, see pipenv above instead.

There are 2 tools (virtualenv and virtualenvwrapper) can be used. There is also one blog introducing bash alias way to use virtualenv: ref ( evernote backup ).
We will use the wrapper one (cuz we prefer new stuff, right?).
If we need to put the env into a folder other than the user's home, we can use the old virtualenv package:
virtualenv [-p /usr/bin/python2.6] <path/to/new/virtualenv/>

install

Win: pip install virtualenvwrapper-win
Nix: pip install virtualenvwrapper (this also works for windows with cygwin)

make new env

mkvirtualenv <env name>
If we want to use another python intepretor, we can use the optional -p (path) parameter:
mkvirtualenv -p D:\Python27\python.exe py27_rest_demo
TIP: After running this command, we will automatically enter the virtual environment.
The name is showing at the beginning of terminal/cmd prompt, surrended by ( ):

OBS: newly created env does NOT have globally installed packages.

using previously created env

workon <env name>

packages

When we are in an env, we can use pip to install packages for this env.

quit current env

deactivate

test package version

Take Django as an example:

python
import django
print(django.get_version())

cmd format

Even with -p parameter, we need to run the .py files with virtual env's python.exe, e.g.:
C:\Users\<user name>\Envs\py27_rest_demo\Scripts\python.exe manage.py runserver
Otherwise, system's global intepretor will take effect.
Easier Way
We can use D:\Python27>mklink python27.exe python.exe to make it possible to run server by: python27.exe manage.py runserver. However, direct copying python.exe and renaming the file does NOT work.

PARALLEL

PERFORMANCE

Simple seconds: time()

import time
start_time = time.time()
# do something here
end_time = time.time()
print(end_time - start_time, 'seconds.')

Alternatives: perf_counter or process_time
ref stackoverflow.

Python in Browser

Use Py instead of js (javascript) by pyodide.
Try directly here.
Maybe also Juliea & R later.

MORE

不可不知的Python陷阱