Python Setup Environment Variables

This article entitle how Python Setup Environment Variables is a continuation of the previous article entitled Python Overview For Absolute Beginners.

Python is a high-level programming language that is available on different Operating Systems (OS) such as Microsoft Windows, Linux, and Mac OS X.

How to set up environment variables in Python?

There are many ways to set up an environment for different Operating Systems and each OS has desired procedures for installation.

The following lists are the different platforms of OS.

  • Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
  • OS/2
  • DOS (multiple versions)
  • Win 9x/NT/2000
  • Windows CE
  • BeOS
  • PalmOS
  • Macintosh (Intel, PPC, 68K)
  • Psion
  • Amiga
  • Nokia mobile phones
  • Acorn/RISC OS
  • VMS/OpenVMS
  • VxWorks
  • QNX

Python is also been relayed to .NET and Java virtual machines

Setup Local Environment

To set up virtual environment variables in Python the first thing that we need to know if check if we have already installed Python on our virtual machines.

Open the terminal window on your local computer and type (python) in order for you to find out if you have already installed it and which version it is. For an instance that you have already installed, you will get a message like this.

Microsoft Windows [Version 10.0.22000.1219]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Glenn>python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Getting Python Ready

The most updated and current installer, documentation, binaries, and everything that you need to know about Python are on their official website python.org.

Installing Python on the local machine

Python is available in a wide variety of Operating Systems. You just need to download the given binary code that is suitable for your platform and then just simply install python.

For instance, if the binary code for your platform is not available, you simply need a C compiler to compile the given source code manually.

Further, compiling the entire source code offers complexity in terms of choosing the features which you require in your installation.

The following are quick overviews of installing Python on different Operating Systems.

Installing Python on Windows Machine

The following is the step-by-step way to install Python on a Windows machine.

  • Go to your favorite web browser and head over and download Python.
  • Simply follow the link of the windows installer, as of now the latest version of Python is Python 3.11.1 which you need to install on your windows machine.
  • To execute the .exe file, the windows machine must be supported by Microsoft Installer 2.0 or Higher. Save the executable file on your local machine and run it in order to find out if the machine supports (MSI).
  • Run the .exe file which will bring up the Python installation wizard which is really easy to install. You just need to accept all the default settings, API keys, and simply wait until the installation is finished.

Installing Python on Unix and Linux Machines

The following is the step-by-step way to install Python on a Unix and Linux machine.

  • Go to your favorite web browser and head over and download Python.
  • Just simply head over and download the executable file available for your Unix and Linux machine.
  • Download and extract the executable files.
  • You can edit the modules or set up the files if you want to customize some features or options.

Now run the following command on your machine.

$ run ./configure script
$ make
$ make install

With the use of the YUM command

The Red Hat Enterprise Linux (RHEL 8) can not install the Python 3 version by default. We usually use the yum command on CentOS and other related development. In order to install the Python 3 version on Red Hat Enterprise Linux (RHEL 8) as follows.

$ sudo yum install python3

Installing Python on Macintosh Machine

Recent Mac OS comes with Python installed, But the problem is it was several years outdated. See this instruction to get the current Python version along with extra tools for the support of development for Mac OS.

Further, for an older Mac OS machine before the Mac OS X 10.3 (released in 2003) Python for Mac is available.

Setting up Python path

Other executable files and programs can be found in many directories, So some operating systems provide a search path that will list all the directories on that operating system for executables.

That path was stored in an environment variable which is named maintained string by the Operating System (OS) module. This path variable contains a piece of information that is available on the command shell and other programs.

The variable path was named PATH in Windows or Path in Unix. Always remember that Unix is case-sensitive, While windows are not.

Further, in Mac OS the installer handles all the details of the path, To use the Python application interpreter in any particular directory, First you must add all the Python directories to your Path directories.

Setting Path in Windows Machine

Python Setup Environment Variables and Python directory to the Path in a particular session on a windows machine.

Open the command prompts and type the path %path%;C:\Python and then press enter.

Just always put In mind that C:\Python is the Path of the given Python directory.

Setting Path in Unix and Linux Machine

The following is the step to add the Python directory to a path in a particular Unix session.

  • Open the csh shell type setenv PATH “$PATH:/usr/local/bin/python” and just simply press enter.
  • Open the bash shell (Linux) type export PATH=”$PATH:/usr/local/bin/python” and just simply press enter.
  • Open the sh or ksh shell ype PATH=”$PATH:/usr/local/bin/python” and just simply press enter.
  • Just always put in mind that the /usr/local/bin/python is the path of the Python directory to raise a keyerror.

Python Environment Variables

The following table below are the important environment variables that are recognizable by Python.

#VariableDescription
1PYTHONPATHIt has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.
2PYTHONSTARTUPIt contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.
3PYTHONCASEOKIt is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.
4PYTHONHOMEIt is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.

Execution of Python

There are only three different ways to run Python.

Interactive interpreter

Python can execute from DOS, Unix, or any other computer system which provides a command-line interpreter or a windows shell.

$python # Unix/Linux
or
python% # Unix/Linux
or
C:> python # Windows/DOS

The following table below is the list of all the command line options.

OptionDescription
-dIt provides debug output.
-OIt generates optimized bytecode (resulting in .pyo text files).
-SDo not run import site to look for Python paths on startup.
-vverbose output (detailed trace on import statements).
-Xdisable class-based built-in exceptions (just use strings); obsolete starting with version 1.6.
-c cmdrun Python script sent in as cmd string
filerun Python script from the given file

Integrated Development Environment

Python can run a Graphical User Interface (GUI) environment once you have a GUI application on your system which supports Python.

  • Windows – Pythonwin is the first windows interface that supports Python and it is an IDE with a Graphical User Interface
  • Unix – The IDLE is the first Unix-integrated development environment (IDE) for Python.
  • Macintosh – IDLE was the first Unix-integrated development environment (IDE) for Python.

Summary

In summary, you have read about the Python Setup Environment Variables. We also discussed in this article how to set up environment variables in Python, set up the local environment, install Python on different platforms, and set up paths to different OS platforms.

I hope this article about Python Setup Environment Variables could help you a lot to continue pursuing learning this powerful programming language. If you want to learn more check out my previous and latest articles for more career-changing articles.

Leave a Comment