Python Environment Setup
PYTHON ENVIRONMENT SETUP – This tutorial will teach you how to set up your own Python Environment on your choice of operating systems like Windows, Linux, and Mac. Python works with many different operating systems, including Mac OS X and Linux. First, let’s get a handle on the basics of configuring a Python environment.
Python Environment Setup: Table of contents
- Python Environment Setup
- What is a Virtual Environment?
- Python Local Environment Setup
- Download Python
- Install Python Environment
- Setting up PATH for Python Environment
- Python Environment Variables
- Running Python Environment
What is a Virtual Environment?
A virtual environment is a Python environment in which the Python interpreter, libraries, and scripts installed are isolated from those installed in other virtual environments, as well as (by default) any libraries installed in a “system” Python, i.e., one that comes with your operating system.
Python Local Environment Setup
Python local environment setup is so basic that it may be done by just opening a terminal window and entering “python” or “py” This will disclose whether or not Python is already installed, as well as the precise version of Python that is presently in use.
Python environment works with many systems, such as 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 (multiple DOS versions), PalmOS, Nokia mobile phones, Windows CE, Acorn/RISC OS, BeOS, Amiga, and VMS/OpenVMS.
To download the official and up-to-date installer of Python you may visit the official Python website, you can find the most up-to-date source code, binaries, documentation, news, etc.
The Python documentation can be found also on the official website of Python. There are HTML, PDF, and PostScript versions of the documentation.
Install Python Environment
Python is available for a lot of different platforms. To install Python, you need to download only the binary code that works for your platform.
If you can’t find the binary code for your platform, you need a C compiler to manually compile the source code. When you compile the source code, you have more freedom to choose which features you want in your installation.
Here is a quick summary of how to set up Python on different platforms:
Unix and Linux Installation
Here are the easy steps to install or setup Python on a computer that uses Unix or Linux.
- Go to Python Download Page in your web browser.
- In order to access the Unix/Linux-compatible, zipped source code, click on the provided link.
- Download and extract files.
- If you want to change some settings, you can do so by editing the Modules/Setup file.
- To configure, run the./configure script.
- make install
With this, Python will be put in the /usr/local/bin directory, and its libraries will be placed in /usr/local/lib/pythonXX, where XX is the version of Python.
Here’s how to install or setup Python on a Windows computer.
- Go to the Python Download Page in your web browser.
- Simply click the provided link and download the python-XYZ.msi file for Windows, where XYZ is the specific version you wish to install.
- The python-XYZ.msi installation requires Microsoft Installer 2.0, which is only available on Windows. To determine if your computer supports the MSI installer, download the file and execute it locally.
- Run the file you downloaded. This displays the Python installation wizard, which is quite user-friendly. Simply accept the default options and wait until the installation is complete.
Python is pre-installed on recent Macs, however, it may be many years out of date. Refer to Python Releases for macOS for information on obtaining the most recent version and additional development tools for the Mac. MacPython is available for earlier Mac OS versions before Mac OS X 10.3 (published in 2003).
Setting up PATH for Python Environment
Setting up PATH for Python Environment on operating systems offers a list of directories where the OS searches for executables. This is because programs and other executable files can exist in multiple directories.
The operating system keeps the path in a variable called an environment variable. The data in this variable can be accessed by the command shell and other applications.
PATH in Unix or Path in Windows describes the path variable. Please note that the Unix is case sensitive while Windows is not.
In Mac OS, the path details are handled by the installer. To run the Python interpreter from any location, the Python directory must be added to your path.
Unix and Linux Path Configuration
The configuration of PATH in Unix and Linux varies depending on the shell.
- Use this command if you are using the csh shell:
# csh shell setenv PATH "$PATH:/usr/local/bin/python"
- Use this command if you are using the bash shell (Linux):
# bash shell (Linux) export PATH="$PATH:/usr/local/bin/python"
- Use this command if you are using the sh or ksh shell:
# sh or ksh shell PATH="$PATH:/usr/local/bin/python"
Note – The path to the Python directory is /usr/local/bin/python.
Windows Path Configuration
This configuration includes the Python directory in the Windows path for a specific session.
- Use this command if you are using the command prompt:
/* command prompt */ path %path%;C:\Python
Note – The path to the Python directory is C:\Python.
Python Environment Variables
Python can read the following important environment variables:
|No.||Variable & Description|
It does the same thing as PATH. This variable tells the Python interpreter where to find the module files that are imported into a program. It should have the Python source library directory and the Python source code directories. The Python installer may have already set the PYTHONPATH variable.
It contains the path to the Python source code initialization file. It is executed each time the interpreter is started. It is named .pythonrc.py under Unix and contains commands that load tools or adjust the PYTHONPATH environment variable.
It instructs Python to locate the first case-insensitive result in an import statement on Windows. To activate this variable, set it to any value.
It is an alternative search path for modules. It is typically included in the PYTHONSTARTUP or PYTHONPATH directories to facilitate library switching.
Running Python Environment
Running Python Environment can be done in one of three possible ways.
Python can be launched from any operating system that provides the user with a command-line or interactive interpreter or shell window, including Unix, DOS, and others.
- First, on the command line, type python.
- Then, you can start writing code right away in Interactive Interpreter.
# Unix/Linux $python # Unix/Linux python% # Windows/DOS C:> python
Here is a list of all the command-line options that you can use.
|no.||Option & Description|
it gives output for debugging purposes.
it creates bytecode that has been optimized (resulting in .pyo files).
do not run import site at startup in order to seek for Python path locations.
verbose output (detailed trace on import statements).
disable class-based built-in exceptions and instead use strings; this feature is no longer supported as of version 1.6.
execute the Python script that was provided as the command string.
execute the Python script located in the supplied file.
Script from the Command Line
You can run a Python script from the command line by calling the interpreter on your application, like this:
# Unix/Linux $python script.py # Unix/Linux python% script.py # Windows/DOS C: >python script.py
Note: Check to make sure the permission mode of the file enables execution.
Integrated Development Environments
You can also run Python using Integrated Development Environments or IDE if your system has a GUI program that supports Python.
- UNIX – IDLE was the very first integrated development environment (IDE) for Python on Unix.
- Windows – PyCharm is the best Windows interface for Python. It’s an integrated development environment (IDE) with a graphical user interface. Which also supports other operating systems.
- Macintosh – The Macintosh version of Python and the IDLE integrated development environment (IDE) are both available for download from the primary website of the Python programming language. The files can be downloaded in either MacBinary or BinHex format.
You can ask your system administrator for assistance in configuring the environment properly if you find that you are unable to do so on your own. Make certain that the Python environment has been correctly installed, setup and is operating normally.
Note: All of the examples in the following chapters are run with the Python 2.4.3 version of Linux that comes with the CentOS flavor.
We have already setup a Python programming environment online so that you can run all the available examples at the same time while you are learning theory. Feel free to change any example and use it online.
In summary, this tutorial will teach you how to setup your own Python Environment on your choice of operating systems like Windows, Linux, and Mac. Python works with many different operating systems, including Mac OS X and Linux.
Setting up PATH for Python Environment on operating systems offers a list of directories where the OS searches for executables. Make certain that the Python environment has been correctly installed, setup, and is operating normally.
In the next tutorial, you will learn about the Python Basic Syntax. I hope that this Python Environment Setup helps you understand how to download, install, and set up PATH for Python programming.