Setting up Python
Python as a programming language
- Interpreted : Python is processed at runtime by the interpreter. You do not need to compile your program before executing it.
- Interactive: You can use Python prompt and interact with the interpreter directly to write your programs.
- Object-Oriented: It supports Object-Oriented style or technique of programming that encapsulates code within objects.
In this post I am going to explain, setting up environment, Installation, and how to run Python.
Setting up Python environment.
Python is available on a wide variety of platforms including,Windows, Linux and Mac OS X.
How to setup Python?
Open a command line and type “python” to find out if it is already installed and which version is installed.
C:\Users\default1>python
If it is installed then, version of Python shall be shown in the output..
Where to downlad Python?
The current source code, binaries, documentation, news, is available on the official website of Python: https://www.python.org/ , and documentation can be downloaded from https://www.python.org/doc/
Installation of Python in different environments
Installation in Windows
Here are the steps to install Python on Windows
- Open a Web browser and go to https://www.python.org/downloads/
- Follow link Windows installer python-XYZ.msi file where XYZ is the version you need to install.
- Use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.
- Run the downloaded file. Comes up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done.
Installation in Linux
- Open a Web browser and go to https://www.python.org/downloads/
- Take the link to download zipped source code available for Unix/Linux.
- Download and extract files
- To customize some options, edit the Modules/Setup file
- run ./configure script
- make install
Now Python is installed at standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the version of Python.
Installation in Mac OS
Python is installed, Recent in Macs come, you can check it in the http://www.python.org/download/mac/ for instructions to get the current version, tools development support.
Setting up PATH in Python
Windows
To add a directory to the path open command prompt and type path
>%path%;C:\Utvecklingprogram\Python\Python_progs
Here the C:\Utvecklingprogram\Python\Python_progs is the path of the Python directoy
Environment Variables
- PYTHONPATH: It 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.
- PYTHONSTARTUP: contains the path of an initialization file containing Python source code, and executed every time you start the interpreter and it is named as pythonrc.py in Linux and it contains commands that load utilities or modify PYTHONPATH.
- PYTHONHOME: this is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.
- PYTHONCASEOK: this is used in Windows to instruct Python to find the first case-insensitive match in an import statement. You can set this variable to any value to activate it.
How to run Python
There are three different ways to run Python :
- Interactive Interpreter
You can run Python from any system that provides you a command-line interpreter or shell window. Enter python the command line and then began coding in the interactive interpreter.
# Windows
C:> python
# Linux
$python
# even in Linux
python%
Command line options :
- -d: provides debug output.
- – O: generates optimized bytecode ( in .pyo files).
- -S: Don’t run import site to look for Python paths on startup.
- -v- output is, detailed trace on import statements.
- -X: disable class-based built-in exceptions (just use strings);
- -c cmd: run Python script sent in as cmd string
- file: run Python script from given file
Command-line script
A Python script can be executed at command line:
# Linux
$python script.py
# Linux
python% script.py
# Windows/DOS
C: >python script.py
Note: file permission mode should be execution.
Integrated Development Environment
if you have a GUI application on your system that supports Python then you can run Python from a Graphical User Interface (GUI) environment as well,
- Windows − PythonWin is the first Windows interface for Python and is an IDE with a GUI.
- Linux − IDLE is the very first Unix IDE for Python.
- Mac os − The Mac version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex’d files.
Conclusion
In this post I have talked about Python language, how to setup, how to install how to run and how to set Path in Python.
In my next post, I will explain the basic stones of Python such as variables, functions, classes, modules, inheritance of object.
This post is part of python-step-by-step