Skip to content

Index

Overview

Learn Python3 and Python2.

Installation

Windows

Download and install Python2 and Python3.

Rename python.exe under directory of Python 2 to python2.exepythonw.exe to pythonw2.exe

Add the directory of Python 2 and Python 3 to the system environment variables, the directory Scripts too.

Execute the following commands in the command line.

python2 -m pip install --upgrade pip --force-reinstall
python3 -m pip install --upgrade pip --force-reinstall

Input pip2 -V and pip3 -V to show the information of version.

Linux

Install related compilation tools.

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install -y libffi-devel zlib1g-dev
yum install zlib* -y

Download installation package of Python. Unzip, make and install Python.

cd /home
wget wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tar.xz
tar -xvJf  Python-3.x.x.tar.xz

# create installation directory
mkdir /usr/local/python3
cd Python-3.x.x
./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl
# prefix: specify installation directory
# enable-optimizations: improve efficiency
# with-ssl: include ssl to help install pip

make && make install

Create symbolic link to the path

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

Verify if installed successfully.

python -V
pip -V

Because of command yum is based on built-in Python2, open the following two files to change "#! /usr/bin/python" to "#! /usr/bin/python2" to redirect to Python2.

vi /usr/bin/yum
vi /usr/libexec/urlgrabber-ext-down

Change the Mirror

Following are common internal mirrors:

  1. Aliyun https://mirrors.aliyun.com/pypi/simple/
  2. Douban https://pypi.douban.com/simple/
  3. THU https://pypi.tuna.tsinghua.edu.cn/simple/
  4. USTC https://pypi.mirrors.ustc.edu.cn/simple/
  5. HUST https://pypi.hustunique.com/

The newest release of Ubuntu requires mirrors from https.

  1. Under Linux, modify ~/.pip/pip.conf as follows:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = https://mirrors.aliyun.com/
  1. Under Windows, create a new file %Users%\username\pip\pip.ini as follows:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = https://mirrors.aliyun.com/

Packaging

We can use py2exe or PyInstaller to package scripts of Python to a executable file.

py2exe

py2exe is an Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.

Download py2exe for Python 2 from SourceForge. Use pip to install that for Python 3 or download it from PyPI.

Create a setup.py under the destination directory along with test.py:

# setup.py
from distutils.core import setup

setup(console=['test.py'])

Then run the setup.py with py2exe from the command line.

python setup.py py2exe

It will create two directories named dist and build, including generated executable file and other files.

PyInstaller

PyInstaller can package Python programs to executable file under Windows, Linux, Mac OS X, Solaris and AIX.

Install it using pip or download it from its website. Note that install PyWin32 before install PyInstaller on Windows.

To package test.py, just run the following command from the command line:

pyinstaller.exe --onefile --windowed test.py
  • --onefile or -F denotes packaging the program into one single executable file. Otherwise, libraries will be distributed as separate file.
  • --windowed or "-w" denotes to show a window when running the generated application.
  • --name can be used to name the application.

Environment

Use virtual environment to simplify the libraries. First, install virtualenv with pip. As follows are commands for example.

virtualenv example_env # create one

virtualenv --system-site-packages example_env # inherit global modules

activate # activate the environment

Or just use PyCharm.

References