Setting Up Python3 Environment
In this chapter, we will guide you through setting up a Python3 development environment on your local machine.
Python3 can be used on multiple platforms including Windows, Linux, and Mac OS X.
- Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
- Win 9x/NT/2000
- Macintosh (Intel, PPC, 68K)
- OS/2
- DOS (various DOS versions)
- PalmOS
- Nokia mobile phones
- Windows CE
- Acorn/RISC OS
- BeOS
- Amiga
- VMS/OpenVMS
- QNX
- VxWorks
- Psion
- Python can also be ported to Java and .NET virtual machines.
Downloading Python3
The latest source code, binary documents, news, and more for Python3 can be found on the official Python website:
Python Official Website: https://www.python.org/
You can download Python documentation from the following link in formats such as HTML, PDF, and PostScript.
Python Documentation Download: https://www.python.org/doc/
Installing Python
Python has been ported to many platforms (with modifications to make it work on different platforms).
You need to download the binary code suitable for your platform and then install Python.
If the binary code for your platform is not available, you need to compile the source code manually using a C compiler.
Compiled source code offers more options and provides greater flexibility for installing Python.
Here are the download links for installation packages on various platforms:
Source Code is available for installation on Linux.
Below are the methods to install Python3 on different platforms.
Installing Python3 on Unix & Linux:
Here are the simple steps to install Python on Unix & Linux platforms:
- Open your web browser and visit https://www.python.org/downloads/source/
- Choose the source tarball suitable for Unix/Linux.
- Download and unzip the tarball Python-3.x.x.tgz, where 3.x.x is the version number you downloaded.
- If you need to customize some options, modify Modules/Setup
For example, for version Python3.6.1:
# tar -zxvf Python-3.6.1.tgz
# cd Python-3.6.1
# ./configure
# make && make install
Check if Python3 is properly installed:
# python3 -V
Python 3.6.1
Installing Python on Windows:
Here are the simple steps to install Python on Windows:
- Open your web browser and visit https://www.python.org/downloads/windows/. Typically, download the executable installer, where x86 is for 32-bit machines and x86-64 is for 64-bit machines.
- Remember to check Add Python 3.6 to PATH.
- Press
Win+R
, typecmd
to open the command prompt, and enterpython
. - Alternatively, you can search for IDLE in the start menu.
Installing Python on Mac:
Mac systems come with Python2.7 pre-installed. You can download the latest version of Python 3.x from https://www.python.org/downloads/mac-osx/.
You can also refer to the source installation method for installation.
Configuring Environment Variables
Programs and executable files can be located in many directories, and these paths may not be included in the search paths provided by the operating system.
Paths are stored in environment variables, which are named strings maintained by the operating system. These variables contain information about available command line interpreters and other programs.
The path variable is named PATH in Unix or Windows (UNIX is case-sensitive, Windows is not).
In Mac OS, the installation process changes the installation path of Python. If you need to reference Python from other directories, you must add the Python directory to your path.
Setting Environment Variables in Unix/Linux
In csh shell: Enter
setenv PATH "$PATH:/usr/local/bin/python"
In bash shell (Linux): Enter
export PATH="$PATH:/usr/local/bin/python"
In sh or ksh shell: Enter
PATH="$PATH:/usr/local/bin/python"
Note: /usr/local/bin/python is the installation directory for Python.
Setting Environment Variables in Windows
Add the Python directory to the environment variables:
In the command prompt (cmd): Enter
path=%path%;C:\Python
Note: C:\Python is the installation directory for Python.
Alternatively, you can set it through:
- Right-click "Computer" and then click "Properties"
- Then click "Advanced system settings"
- Select "Path" under the "System Variables" window and double-click it!
- Then in the "Path" line, add the Python installation path (e.g., D:\Python32), so append this path at the end. PS: Remember, paths are separated by semicolons ";"!
- After successful setup, typing "python" in the cmd command line will show relevant information.
Python Environment Variables
The following important environment variables are used by Python:
Variable Name | Description |
---|---|
PYTHONPATH | PYTHONPATH is the Python search path; modules imported by default are searched within PYTHONPATH. |
PYTHONSTARTUP | Python looks for PYTHONSTARTUP environment variable and executes the code in the specified file before starting. |
PYTHONCASEOK | Setting PYTHONCASEOK makes Python import modules without case sensitivity. |
PYTHONHOME | Another module search path. It is usually embedded in PYTHONSTARTUP or PYTHONPATH directories, making it easier to switch between module libraries. |
Running Python
There are three ways to run Python:
1. Interactive Interpreter:
You can start coding in Python by entering the interactive interpreter through the command line.
You can do this on Unix, DOS, or any system that provides a command line or shell.
$ python # Unix/Linux
or
C:>python # Windows/DOS
Here are the Python command line options:
Option | Description |
---|---|
-d | Display debug information during parsing |
-O | Generate optimized code (.pyo files) |
-S | Do not import the Python path location at startup |
-V | Output the Python version number |
-X | Deprecated since version 1.6 for built-in exceptions (only for strings). |
-c cmd | Execute a Python script, treating the output as a cmd string. |
file | Execute a Python script from the given file. |
2. Command Line Script
You can execute Python scripts from the command line by invoking the interpreter, as shown:
$ python script.py # Unix/Linux
or
C:>python script.py # Windows/DOS
Note: Ensure the script has executable permissions when running.
3. Integrated Development Environment (IDE): PyCharm
PyCharm is a Python IDE created by JetBrains, supporting macOS, Windows, and Linux systems.
PyCharm features: Debugging, syntax highlighting, project management, code navigation, smart hints, auto-completion, unit testing, version control...
PyCharm download link: https://www.jetbrains.com/pycharm/download/
PyCharm installation guide: http://www.tutorialpro.org/w3cnote/pycharm-windows-install.html Professional (Professional Edition, paid): Full functionality, 30-day trial available.
Community (Community Edition, free): A stripped-down version of the Professional Edition.