Online Python IDE
Today, you can learn Python online without installing Python on your computer. You can sign up at one of the online Python IDE (Integrated Development Environment), providing a real programming environment in a browser, or Software as a Service (SaaS).
The real benefit for using an online IDE is collaborative coding. Otherwise, you probably should install your own IDE, if you are programming day-in and day-out. Nonetheless, we need to compare with collaborating via GitHub.
Replit Online IDE
Replit (formerly Repl.it) @ https://replit.com/ is an online IDE, allowing users to create online projects and do collaborative coding. It supports over 50 programming languages including Java, JavaScript, Node.js, Python, PHP, Perl, Go, C/C++, C#. It also supports debugger, unit testing, and GitHub repository.
The free ($0) plan supports unlimited public project, which is a good option to learn and start programming. Fee plan supports private project.
Its name comes from the acronym REPL, which stands for "Read-Evaluate-Print Loop".
Anaconda Python Distribution
From Wiki: "Anaconda @ https://www.anaconda.com/ is a distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to simplify package management and deployment. Anaconda distribution comes with over 250 packages automatically installed, and over 7,500 additional open-source packages can be installed from PyPI as well as the conda package and virtual environment manager. It also includes a GUI, Anaconda Navigator, as a graphical alternative to the command-line interface (CLI)."
It also provide an online cloud-hosted notebook service for coding Python programs.
Installing Python
Plain Python from Python Mother Site @ python.org
You can install plain Python from the Python mother site:
- Check out Python mother site @ https://www.python.org/.
- Read "Beginner's Guide to Python" @ https://wiki.python.org/moin/BeginnersGuide.
- To download Python 3, goto https://www.python.org/downloads/, download the Python for your operating system (Windows, macOS, Linux) and install.
- For Windows: Python is installed under "C:\Users\username\AppData\Local\Programs\Python\Python311".
- For macOS: [TODO]
- For Linux: [TODO]
- The installation includes:
- Python Command-Line Interface (CLI)
- Python IDLE IDE
- Manual and Documentation
- Check out the:
- "The Python 3 Tutorial" @ https://docs.python.org/3/tutorial/index.html
- "The Python 3 Language Reference" @ https://docs.python.org/3/reference/
Launching Python Command-Line Interface (CLI)
To launch the Python CLI, use "py
" in windows (kept in C:\Windows\py.exe
), and "python3
" for macOS/Linux.
# Windows > py --version Python 3.11.3 > py Python 3.11.3 (....) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
# macOS/Linux
$ python3 --version
......
$ python3
......
For Windows
- Python is installed under "C:\Users\username\AppData\Local\Programs\Python\Python311".
- "
py
" is kept inC:\Windows\py.exe
(you can check via command "where py
". - The Start button has shortcut to launch Command-Line Interface (CLI) and IDLE.
Using VS Code IDE
VS Code (or Visual Studio Code), is a source-code editor made by Microsoft for Windows, Linux and macOS, released in 2015. It supports debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git. Users can change the theme, shortcuts, preferences, and install extensions. It supports many languages, including C/C++, C#, Java, Python, JavaScript, and etc.
VS Code was ranked the most popular developer environment in the Stack Overflow Developer Survey since 2018.
See "VS Code for Python".
Using IDLE IDE
Besides the Command-Line Interface (CLI), the plain Python comes with a simple IDE called IDLE.
[TODO]
Launching pip (Python Package Manager)
# Windows > py -m pip --version pip 22.3.1 from C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\site-packages\pip (python 3.11) > py -m pip install "SomeProject" ......
# macOS/Linux
$ python3 -m pip --version
......
Using VS Code for Python Development
[TODO]
Anaconda Python distribution
"Anaconda @ https://www.anaconda.com/ is a distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to simplify package management and deployment. Anaconda distribution comes with over 250 packages automatically installed, and over 7,500 additional open-source packages can be installed from PyPI as well as the conda package and virtual environment manager. It also includes a GUI, Anaconda Navigator, as a graphical alternative to the command-line interface (CLI)." - Wiki.
For Newcomers to Python, I suggest you install "Anaconda distribution of Python 3", which includes a Command-Line Interface (CLI), IDEs (Jupyter Notebook and Spyder), and bundled with commonly-used packages (such as NumPy, Matplotlib and Pandas that are used for data analytics).
Goto Anaconda mother site (@ https://www.anaconda.com/) ⇒ Choose "Anaconda Distribution" Download ⇒ Choose "Python 3.x" ⇒ Follow the instructions to install.
Check If Python Already Installed and its Version
To check if Python is already installed and its the version, issue the following command:
$ python3 --version # or "py" or "python"
Python 3.5.2
Ubuntu (16.04LTS)
Both the Python 3 and Python 2 should have already installed by default. Otherwise, you can install Python via:
# Installing Python 3
$ sudo apt-get install python3
To verify the Python installation:
# Locate the Python Interpreters $ which python3 /usr/bin/python3 $ ll /usr/bin/python* lrwxrwxrwx 1 root root 9 xxx xx xxxx python -> python2.7* lrwxrwxrwx 1 root root 9 xxx xx xxxx python2 -> python2.7* -rwxr-xr-x 1 root root 3345416 xxx xx xxxx python2.7* lrwxrwxrwx 1 root root 9 xxx xx xxxx python3 -> python3.5* -rwxr-xr-x 2 root root 3709944 xxx xx xxxx python3.5* -rwxr-xr-x 2 root root 3709944 xxx xx xxxx python3.5m* lrwxrwxrwx 1 root root 10 xxx xx xxxx python3m -> python3.5m* # Clearly, # "python" and "python2" are symlinks to "python2.7". # "python3" is a symlink to "python3.5". # "python3m" is a symlink to "python3.5m". # "python3.5" and "python3.5m" are hard-linked (having the same inode and hard-link count of 2), i.e., identical.
Windows
You could install either:
- Anaconda Distribution of Python
- Plain Python from Python mother site @ https://www.python.org/download/, download the installer, and run the downloaded installer.
- Under the Cygwin (Unix environment for Windows) and install Python (under the "devel" category).
macOS
[TODO]
Documentation
Python documentation and language reference are provided online @ https://docs.python.org.
Getting Started with Python
Python Interpreter in Command-Line Interface (CLI)
You can run the "Python Interpreter" in interactive mode under a "Command-Line Interface (CLI)" (such as Anaconda Prompt, Windows' CMD, macOS's Terminal, Ubuntu's Bash Shell):
$ python # Try "py", "python3", "python2" and check its version
Python 3.7.0
......
Type "help", "copyright", "credits" or "license" for more information.
>>>
The Python's command prompt is denoted as >>>
. You can enter Python statement at the Python's command prompt, e.g.,
>>> print('hello, world') hello, world # 2 raises to power of 88. Python's int is unlimited in size >>> print(2 ** 88) 309485009821345068724781056 # Python supports floating-point number >>> print(8.01234567890123456789) 8.012345678901234 # Python supports complex number >>> print((1+2j) * (3+4j)) (-5+10j) # Create a variable with an numeric value >>> x = 123 # Show the value of the variable >>> x 123 # Create a variable with a string >>> msg = 'hi!' # Show the value of the variable >>> msg 'hi!' # Exit the interpreter >>> exit() # or Ctrl-D or Ctrl-Z+Enter
To exit Python Interpreter:
exit()
- (macOS and Ubuntu)
Ctrl-D
- (Windows)
Ctrl-Z
followed by Enter
Writing and Running Python Scripts
First Python Script - hello.py
Use a programming text editor to write the following Python script and save as "hello.py
" in a directory of your choice:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
hello: First Python Script
"""
myStr = 'Hello, world' # Strings are enclosed in single quotes or double quotes
print(myStr)
myInt = 2 ** 88 # 2 to the power of 88. Python's integer is unlimited in size!
print(myInt)
myFloat = 8.01234567890123456789 # Python support floating-point numbers
print(myFloat)
myComplex = (1+2j) / (3-4j) # Python supports complex numbers!
print(myComplex)
myLst = [11, 22, 33] # Python supports list (dynamic array)
print(myLst[1])
How it Works
- By convention, Python script (module) filenames are in all-lowercase (e.g.,
hello
). - EOL Comment: Statements beginning with a
#
until the end-of-line (EOL) are comments. - #!/usr/bin/env python3 (Line 1) is applicable to the Unix environment only. It is known as the Hash-Bang (or She-Bang) for specifying the location of Python Interpreter, so that the script can be executed directly as a standalone program.
- # -*- coding: UTF-8 -*- (Line 2, optional) specifies the source encoding scheme for saving the source file. We choose and recommend UTF-8 for internationalization. This special format is recognized by many popular editors for saving the source code in the specified encoding format.
- """ hello ...... """ (Line 3-5): The script begins by the so-called doc-string to provide the documentation for this Python module. Doc-string is typically a multi-line string (delimited by triple-single or triple-double quoted), which can be extracted from the source file to create documentation.
- Variables: We create variables
myStr
,myInt
,myFloat
,myComplex
,myLst
(Line 6, 8, 10, 12, 14) by assignment values into them. - Python's strings can be enclosed with single quotes
'...'
(Line 6) or double quotes"..."
. - Python's integer is unlimited in size (Line 8).
- Python support floating-point numbers (Line 10).
- Python supports complex numbers (Line 12) and other high-level data types.
- Python supports a dynamic array called list (Line 14), represented by
lst=[v1, v2, ..., vn]
. The element can be retrieved via indexlst[i]
(Line 15). - print(aVar): The
print()
function can be used to print the value of a variable to the console.
Expected Output
The expected outputs are:
Hello, world 309485009821345068724781056 8.012345678901234 (-0.2+0.4j) 22
Running Python Scripts
You can develop/run a Python script in many ways - explained in the following sections.
Running Python Scripts in Command-Line Shell (Anaconda Prompt, CMD, Terminal, Bash)
You can run a python script via the Python Interpreter in the Command-Line Interface (CLI):
$ cd <dirname> # Change directory to where you stored the script $ python hello.py # Run the script via the Python interpreter # (Also try "py hello.py", "python3 hello.py" and "python2 hello.py"
Unix's Executable Shell Script
In Linux/macOS, you can turn a Python script into an executable program (called Shell Script or Executable Script) by:
- Start with a line beginning with
#!
(called "hash-bang" or "she-bang"), followed by the full-path name to the Python Interpreter, e.g.,#!/usr/bin/python3 ......
To locate the Python Interpreter, use command "which python
" or "which python3
". - Make the file executable via
chmod
(change file mode) command:$ cd /path/to/project-directory $ chmod u+x hello.py # enable executable for user-owner $ ls -l hello.py # list to check the executable flag -rwxrw-r-- 1 uuuu gggg 314 Nov 4 13:21 hello.py
- You can then run the Python script just like any executable programs. The system will look for the Python Interpreter from the she-bang line.
$ cd /path/to/project-directory $ ./hello.py
The drawback is that you have to hard code the path to the Python Interpreter, which may prevent the program from being portable across different machines.
Alternatively, you can use the following to pick up the Python Interpreter from the environment:
#!/usr/bin/env python3
......
The env
utility will locate the Python Interpreter (from the PATH
entries). This approach is recommended as it does not hard code the Python's path.
Windows' Executable Program
In Windows, you can associate ".py
" file extension with the Python Interpretable, to make the Python script executable.
Running Python Scripts inside Python's Interpreter
To run a script "hello.py
" inside Python's Interpreter:
# Python 3 and Python 2 $ python3 ...... >>> exec(open('/path/to/hello.py').read()) # Python 2 $ python2 ...... >>> execfile('/path/to/hello.py') # OR >>> exec(open('/path/to/hello.py'))
- You can use either absolute or relative path for the filename. But,
'~'
(for home directory) does not work?! - The
open()
built-in function opens the file, in default read-only mode; theread()
function reads the entire file.
Python IDEs and Debuggers
Picking a IDE with a powerful graphic debugger is CRITICAL in program development!!!
VS Code
[TODO]
Python's Anaconda Distribution (Recommended for Data Science)
Besides the Official Python Distribution from python.org, there are many Python distributions available. Among them, the open-source Anaconda Python Distribution (@ https://www.anaconda.com/) is probably the most popular for data science, by bundling:
- IDEs: Jupyter Notebook, Spyder
- Analytics & Scientific Computing Python Packages: NumPy, Matplotlib, Pandas, SciPy
- Machine Learning Python Packages: scikit-learn
It is available on Linux, Windows, and Mac OS X.
Installation
Goto Anaconda's mother site @ https://www.anaconda.com/. Download "Anaconda Distribution". Choose the Python 3 version. Follow the instructions to install.
Getting Started
The Anaconda Distribution comes with:
- Anaconda Prompt: a Command-line shell (CMD, Terminal, Bash Shell) for launching the Python Interactive Interpreter.
(base) xxx> python Python 3.7.0 (default) [MSC v.1912 64 bit (AMD64)] :: Anaconda custom (64-bit) on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
- Jupyter Notebook: a web-based IDE suitable for data analytics by combining code, documentation, and visual into notebook.
- Spyder: a MatLab-like IDE.
- Anaconda Navigator
Explore "Get Started" section, under https://www.anaconda.com/download/.
Conda Package Management System
Anaconda uses the open-source conda package management system for managing packages and their dependencies (install and update), and the environment.
You need to run Conda under the "Anaconda Prompt". To install or update packges, you may need the administrator/superuser right:
- For Windows, right-click and "run as administrator"
- For Linux and Mac OS X, run the commands with "
sudo
".
Using Conada:
- Conda's commands:
# Show help menu $ conda help # Show help for command $ conda command --help # Clean up the conda installation - remove cache, outdated packages, zip files $ conda clean --all # Show info about Anaconda $ conda info # Show all packages $ conda list # Install a package $ conda install package-name # Update a package $ conda update package-name
- To install a package under Anaconda, start a Anaconda Prompt (as administrator or with
sudo
) and issue:$ conda install --help $ conda install package_name # For examples, $ conda install numpy $ conda install pandas
- To update a package, start a Anaconda Prompt (as administrator or with
sudo
) and issue:$ conda update --help # Update Anaconda distribution $ conda update anaconda # Update a Python package $ conda update numpy $ conda update pandas $ conda update matplotlib $ conda update scikit-learn $ conda update jupyter
- (For Windows) You can change the start-up directory of "Anaconda Prompt", by right-right on the program ⇒ Properties ⇒ Shortcut ⇒ change the "start-in".
Jupyter Notebook (Recommended for Data Science Projects)
The Jupyter Notebook (@ http://jupyter.org/) is an open-source web application that allows you to create and share notebooks. According to Wiki, "A notebook is a book or binder of paper pages, often ruled, used for purposes such as recording notes or memoranda, writing, drawing or scrapbooking." A Jupyter Notebook (just like a paper notebook) integrates code and its output into a single document that combines visualizations, narrative text, mathematical equations, and other rich media.
Jupyter Notebook is an easy-to-use data science tool for beginners and educators, in particular for data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning. It supports many programming languages, of which Python is the most commonly-used.
Jupyter Notebook is a successor to the earlier iPython Notebook, started in 2010.
Installation
Follow the instructions at the Jupyter mother site (@ http://jupyter.org/). Download and install "Anaconda" (Python 3 version), which is the most widely used Python distribution for data science, bundled with Jupyter Notebook and Python data science libraries such as NumPy, pandas and Matplotlib.
Getting Started
References: Read "Jupyter Notebook for Beginners: A Tutorial" @ https://www.dataquest.io/blog/jupyter-notebook-tutorial/, which has a simple data science example.
- Launch "Jupyter Notebook", which is a webapp that opens a new tab in your default web browser, showing the so-called "Jupyter Dashboard", with URL
http://localhost:8888/tree
listing the default start-up directory. - The default start-up directory is probably not your desired working directory. To change the start-up directory, shutdown the previous app (Press Ctrl-C twice at the console). Launch the Anaconda Prompt from the Start menu (Windows). Change directory to your desired start-up directory and run "
jupyter notebook
". - To create a new notebook for Python, click "New" and select "Python". A new notebook is opened in a new tab. It is saved as "
Untitled.ipynb
" (switch back to the Dashboard to see this new file in the directory). Each.ipynb
file is a text file that describes the contents of your notebook in JSON format. You can check its contents later after performing some operations. - Jupyter's code is organized in fragments called cells. You can create/remove cells and re-arrange them to create your desired notebook.
- Type "
print('Hello World!')
" into the active cell labeledIn[ ]
, and click "run" (or pressCtrl+Enter
). The label changes fromIn[]
toIn[1]
. The number1
indicates when the cell was executed on the kernel. Run the cell again, it will change toIn[2]
. Take note of the printed output below the code. - Create a new cell by choosing menu "Insert" ⇒ "Insert cell below", or click the
'+'
icon. Enter "print(2 ** 88)
" and Run. - Create a new new cell. Enter the following code. Run and take note of the "
Out[n]
" box, which contains the return value of the code.def say_hello(who): return 'Hello, {}!'.format(who) say_hello('Peter')
- Create a new cell. Enter the following code.
import time time.sleep(5)
Run and observe thatIn[ ]
changes toIn[*]
(indicating the cell is currently running thesleep()
), and thenIn[n]
when completed. - To run a Python script, enter either:
%run -i script_name
(without the.py
)exec(open('script_name.py').read())
- Jupyter runs in two modes: edit mode (with green left-border for the active cell and a "pen" icon displayed on top-right corner) and command mode (with blue left-border). You can switch between these 2 modes by clicking inside/outside the active cell.
- In "command mode", you can issue these commands (with their respective keyboard shortcuts):
- Keys
'a'
and'b'
to insert a cell above and below the active cell. - Keys
'd'+'d'
('d'
twice) to delete the active cell, and'z'
to undo the delete. Shift+Up/Down
(orShift+Click
the margin) to select multiple cells, andShift+'m'
to merge the cells.
- Keys
- In "edit mode", you can issue these commands (with their respective keyboard shortcuts):
Ctrl+Shift+'-'
to split the active cell at the cursor.
- To rename a notebook, you need to go to the Dashboard. Select the notebook, choose "Shutdown" to shutdown the kernel. Select the notebook again, then "Rename".
- To shutdown Jupyter Notebook, press Ctrl-C twice at the console.
Cells
A cell contains program code or documentation text. Jupyter Notebook supports two types of cells:
- Code Cell: contains program code to be executed in the kernel. The output is displayed below the cell.
- Markdown Cell: contains text formatted using Markdown (a lightweight version of HTML - to be explained below). The output is displayed in-place when it is run.
Kernel
A kernel is a computational engine that executes the code contained in a notebook. Each notebook runs a kernel, which maintains the state of a notebook's computations. When you run a code cell, that code is run inside the kernel and the output is returned back to the cell to be displayed. The kernel's state persists over time and between cells. A variable defined or a module imported in one cell is available to another cell. Take note that cells are ordered which corresponds to the state of the kernel. From the "Kernel" menu, you can:
- Restart: restart the kernel. Clearing all the variables.
- Restart & Clear Output: Also clear the output displayed below the code cells.
- Restart & Run All: Also run all your codes from top to bottom.
- Interrput: If your kernel is stuck on a computation and you wish to stop it.
- Shutdown:
Jupyter Notebook support many types of kernel. Beside the various version of Python, it supports over 100 languages including Java, C, R, Julia, MatLab, and SoS multi-language kernel.
Formatting Text with Markdown
Markdown cells are meant for including formatted documentation for your notebook. Markdown is a lightweight (or simplified or subset) of HTML, with very few markup tags for formatting documents, e.g., line beginning with #
for <h1>
, ##
for <h2>
, **text**
for <strong>
, __text__
for <em>
, etc.
For example, insert a new cell, select "Markdown" from the pull-down menu, and enter the following:
# This is a level 1 heading or h1 in HTML ## This is a level 2 heading or h2 in HTML ### This is a level 3 heading or h3 in HTML This is a paragraph or p in HTML. Mark text as strong via **strong** and __strong__, or em via *emphasis* and _emphasis_. Separate paragraphs by an empty line. Below is an unordered list (marked by a beginning star): * level 1 * level 2 (indented) Below is an order list (marked by numbers): 1. Item 1 2. Item 2 Markdown cell supports normal HTML as well. <h1>Level 1 Heading</h1> <p>A paragraph with <strong>strong</strong> and <em>emphasis</em>.</p>
To show the actual layout, run the cell or press Ctrl-Enter
.
Take note that the Markdown cells support the normal HTML as well, which many people are more familiar and comfortable.
Debugging
HARD!!! Use Spyder or IDLE instead, which supports breakpoint and stepping.
Spyder
Spyder (Scientific Python Development Environment) (@ https://docs.spyder-ide.org/index.html) is an IDE that has similar features to the IDE of Matlab.
"Spyder is a powerful scientific environment written in Python, for Python, and designed by and for scientists, engineers and data analysts. It features a unique combination of the advanced editing, analysis, debugging, and profiling functionality of a comprehensive development tool with the data exploration, interactive execution, deep inspection, and beautiful visualization capabilities of a scientific package. Furthermore, Spyder offers built-in integration with many popular scientific packages, including NumPy, SciPy, Pandas, IPython, QtConsole, Matplotlib, SymPy, and more."
Spyder is bundled in Python's Anaconda distribution, can be installed together with Jupyter Notebook.
Getting Started
Spyder IDE contains an Editor and a IPython Console. You can use the editor to edit and run a Python script, or use the IPython Console to issue a Python statement. The editor support syntax highlighting, content-assist, and many productivity features. You can call out the online help.
Debugging
The editor supports debugging. You can set breakpoints by double-clicking on the left margin, step through or run to the statements, and watch the variables and outputs.
IDLE (Recommended for Beginners)
Python IDLE (Interactive DeveLopment Environment) is a simple IDE with features such as syntax highlighting, automatic code indentation and visual debugger.
Installing/Launching IDLE
- For Ubuntu: To install Python IDLE for Python 2 and Python 3, respectively:
# Install IDLE for Python 2 $ sudo apt-get install idle # Install IDLE for Python 3 $ sudo apt-get install idle3 # Verify the installation $ which idle /usr/bin/idle $ which idle3 /usr/bin/idle3 $ ll /usr/bin/idle* -rwxr-xr-x 1 root root 91 xxx xx xxxx /usr/bin/idle* -rwxr-xr-x 1 root root 92 xxx xx xxxx /usr/bin/idle3* -rwxr-xr-x 1 root root 94 xxx xx xxxx /usr/bin/idle-python2.7* -rwxr-xr-x 1 root root 94 xxx xx xxxx /usr/bin/idle-python3.5* $ dpkg --status idle Version: 2.7.11-1 ...... $ dpkg --status idle3 Version: 3.5.1-3 ......
To launch IDLE for Python 2 or Python 3:$ idle $ idle3
- For Windows: IDLE is bundled in the installation. Click the START button ⇒ Python ⇒ IDLE (Python GUI). To exit, choose "File" menu ⇒ Exit. IDLE is written in Python and is kept under "
Lib\idlelib
". You can also use "idle.bat
", "idle.py
", "idle.pyw
" to start the IDLE. - For Mac OS X: [TODO]
Using IDLE
In Python IDLE, you can enter Python statements interactively, similar to interactive Python command-line shell. E.g.,
$ idle3
Python 3.5.2 [GCC 5.4.0 20160609] on Linux Type "help", "copyright", "credits" or "license" for more information. >>> print('Hello, world') Hello, world >>> print(2 ** 88) 309485009821345068724781056 >>> print(8.01234567890123456789) 8.012345678901234 >>> print((1+2j) * (3-4j)) (-5+10j) >>>
Writing Python Script in IDLE
To write a Python script under IDLE, choose "File" menu ⇒ "New File". Enter the above script and save as "hello.py
" in a directory of your choice (the Python script must be saved with the ".py
" extension). To run the script, choose "Run" Menu ⇒ "Run Module". You shall see the outputs on the IDLE console.
Notes:
- You can use Alt-P/Alt-N to retrieve the previous/next command in the command history.
- You can read the Python Manual via the "Help" menu.
Debugging Python Script in IDLE
- In the main IDLE console, choose "Debug" ⇒ "Debugger" to pop out the "Debug Control" and turn on the debugging mode.
- In the script's edit window, choose "Run" ⇒ "Run Module" to start debugging the script. Tick "Source".
- You can set a breakpoint by right-click the source line, choose "Set Breakpoint" (or "Clear Breakpoint").
- From the "Debug Control", You can step over (Over), step into (Step), step out (Out), etc.
Eclipse PyDev (Recommended for Web Projects)
There are several Eclipse plug-ins for Python - notably PyDev @ http://www.pydev.org/.
Installing PyDev
To install PyDev plug-in for Eclipse: Launch Eclipse ⇒ Help ⇒ Install New Software ⇒ In "Work With", enter "http://pydev.org/updates" ⇒ Add ⇒ Select "PyDev".
Caution: When you are prompted for PyDev certificate, you MUST manually select (check) the certificate, before pressing OK. Otherwise, the installation aborts without any warning.
Configuring PyDev
After the installation, you need to configure Python Interpreter: Launch Eclipse ⇒ Window ⇒ Preference ⇒ PyDev ⇒ Expand "Interpreters" node ⇒ Python Interpreter ⇒ New to configure your Python Interpreter. For examples, in Ubuntu, /usr/bin/python
for Python 2.7 or /usr/bin/python3
for Python 3.5; in Windows, python.exe
. You can configure many interpreters and choose the desire one for each of your project.
Writing Python Script using PyDev
To start a new Python project: Launch Eclipse ⇒ File ⇒ New ⇒ Project ⇒ PyDev ⇒ PyDev Project ⇒ Enter "Project name", e.g., "HelloPython" ⇒ Choose the "Interpreter" (configured earlier) and "Grammar Version" ⇒ "Finish".
To write a Python script: Right-click on the project ⇒ New ⇒ PyDev Module ⇒ Leave the "Package" empty ⇒ In "Name", enter "hello" ⇒ Key in the following script:
"""First Python module to say Hello"""
def sayHello(name):
return "Hello, " + name
print(sayHello('Peter'))
Running Python Script
To run the script, right-click on the script ⇒ Run As ⇒ Python Run.
Debugging Using PyDev
To debug a script, set a breakpoint by double-clicking on the left-margin of the the desired line ⇒ right-click on the script ⇒ Debug as ⇒ Python run.
You can then trace the execution via "Step Over (current statement) (F5)", "Step Into (function) (F6)", "Step Out (function) (F7)", "Resume (to next breakpoint) (F8)", "Terminate", and etc.
PyCharm (Recommended for Web Projects)
[TODO]
Programming Text Editors
Sublime Text
[TODO]
Atom
[TODO]
Python Tools
python-dev
- Python development tools
Many of the Python packages require python-dev
(Python development tools) to install and build.
(Ubuntu) Installing python-dev
# Python 3
$ sudo apt-get install python3-dev
......
The following NEW packages will be installed:
libpython3-dev libpython3.5-dev python3-dev python3.5-dev
......
$ dpkg --status python3-dev
Version: 3.5.1-3
......
pip
- Python Package Manager
References
pip
@ https://pypi.python.org/pypi/pip.pip
documentation @ https://pip.readthedocs.org/en/stable.
pip
(Python Package Manager) is used for downloading, installing/un-installing, and managing Python packages. It is a replacement for an earlier tool called easy_install
.
(Ubuntu) Installing pip
There are two versions of pip
: pip2
for Python 2 and pip3
for Python 3. The pip
executable could be symlinked to either pip2
or pip3
.
# (For Python 3) Install 'pip3' $ sudo apt-get install python3-pip ...... $ which pip3 /usr/bin/pip3 $ ll /usr/bin/pip* -rwxr-xr-x 1 root root 307 Nov 18 20:28 /usr/bin/pip3* $ pip3 --version pip 18.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6) # (For Python 2) Install 'pip2' $ sudo apt-get install python-pip ...... $ which pip2 /usr/bin/pip2 $ ll /usr/bin/pip* -rwxr-xr-x 1 root root 306 xxx xx xx:xx /usr/bin/pip* -rwxr-xr-x 1 root root 306 xxx xx xx:xx /usr/bin/pip2* -rwxr-xr-x 1 root root 307 xxx xx xx:xx /usr/bin/pip3* $ pip2 --version pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7) $ pip --version pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Take note that pip2
uses Python 2; while pip3
uses Python 3.
Upgrading pip
The apt-get install
may not install the latest pip
version. You could upgrade pip
using pip
itself, as follows:
# Python 3
$ sudo pip3 install --upgrade pip
$ pip3 --version
......
Note: If you use 'sudo'
in the above commands, the upgrades will be installed in /usr/lib
, which will be available system-wide and for all users. On the other hand, if you did not use 'sudo'
, the upgrades were installed in /home/username/.local/lib
, which is meant for the current user.
Using pip
Installing a package:
# Syntax: pip install <package-name>
$ pip install virtualenv
Downloading/unpacking virtualenv
Downloading virtualenv-13.1.2-py2.py3-none-any.whl (1.7MB): 1.7MB downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...
Showing what files were installed and where:
# Syntax: pip show --files <package-name>
$ pip show --files virtualenv
Name: virtualenv
Version: 15.1.0
Location: /usr/local/lib/python2.7/dist-packages
......
Installing a package of a specific or minimum version:
# Syntax for installing specific version: pip install <package-name>==<version> # Syntax for installing minimum version: pip install <package-name>>=<version> $ pip install virtualenv==13.1.1 # specific version $ pip install virtualenv>=13.1.1 # minimum version
Installing a package from a URL:
Syntax: pip install <url>
$ pip install https://github.com/pypa/virtualenv/archive/develop.tar.gz
Listing all/out-dated packages:
# List all installed packages $ pip list # List out-dated packages $ pip list --outdated requests (Current: 2.2.1 Latest: 2.8.1) ......
Upgrading a package:
# Syntax: pip install --upgrade <package-name>
$ pip install --upgrade requests
Un-Installing a package:
# Syntax: pip uninstall <package-name>
$ pip uninstall requests
Search for a package:
# Syntax: pip search <package-name>
$ pip search requests
Installing package for a user:
# Syntax: pip install --user <package-name> $ pip install --user requests # The package will be installed in $HOME/.local/lib/python2.7/site-packages
Managing a list of packages at specific version numbers
The most important feature of pip
is to manage a full list of packages at specific version numbers, so as to synchronize two environments (such as the development and production environments). Furthermore, via the so-called virtual environment (to be described next), each Python application can maintain its own list (and versions) of packages.
To create a list of pip-installed packages in a requirement file:
# Syntax: pip freeze > [list-name.txt]
$ pip freeze > requirements.txt
The requirement file contains a list of packages and their version numbers, e.g.,
apturl==0.5.2 beautifulsoup4==4.4.1 blinker==1.3 ......
You can then duplicate this list of packages in another machine:
# Syntax: pip install -r [list-name.txt]
$ pip install -r requirements.txt
virtualenv
- Virtual Environment for Python Application Development
References
virtualenv
@ https://pypi.python.org/pypi/virtualenv.virtualenv
documentation @ https://virtualenv.readthedocs.org/en/latest.
A Python "Virtual Environment" is a dedicated directory which contains everything that a Python application needed to run the application in an isolated environment. It includes its own Python interpreter, its own pip
and all the pip
-installed packages. Via virtual environments, you can test and run different Python applications with different Python versions and packages. You can also easily synchronize the development Python environment and the production Python environment.
(Ubuntu) Installing virtualenv
To install virtualenv
via pip
2 (for Python 2) or pip3
(for Python 3):
$ sudo pip2 install virtualenv
$ sudo pip3 install virtualenv
# Verifying the installation
$ pip2 show --files virtualenv
$ pip3 show --files virtualenv
Name: virtualenv
Version: 15.1.0
Location: /usr/local/lib/python2.7/dist-packages
......
$ which virtualenv
/usr/local/bin/virtualenv
$ virtualenv --version
15.1.0
Using virtualenv
Suppose that we start a new Python project in the development environment, and would like the project to run under specific versions of Python Interpreter and packages, which is isolated from the system environment and can be duplicated to the production environment. We can create a virtual environment for this project, which is basically a directory keeping its own Python Interpreter and packages isolated from the system packages and can be duplicated in another environment.
For example, to create a virtual environment in Python 2:
# Goto the project directory $ cd /path/to/project-directory # Create a virtual environment called 'myvenv' for this project, # which is a directory containing its Python interpreter and packages. # Syntax: virtualenv <env-folder-name> $ virtualenv myvenv New python executable in myvenv/bin/python Installing setuptools, pip, wheel...done. $ ll myvenv drwxrwxr-x 2 xxxx xxxx 4096 xxx xx xx:xx bin/ drwxrwxr-x 2 xxxx xxxx 4096 xxx xx xx:xx include/ drwxrwxr-x 3 xxxx xxxx 4096 xxx xx xx:xx lib/ drwxrwxr-x 2 xxxx xxxx 4096 xxx xx xx:xx local/ -rw-rw-r-- 1 xxxx xxxx 60 Feb 7 23:50 pip-selfcheck.json $ ll myvenv/bin -rw-rw-r-- 1 xxxx xxxx 2253 xxx xx xx:xx activate -rwxrwxr-x 1 xxxx xxxx 242 xxx xx xx:xx pip* -rwxrwxr-x 1 xxxx xxxx 242 xxx xx xx:xx pip2* -rwxrwxr-x 1 xxxx xxxx 242 xxx xx xx:xx pip2.7* -rwxrwxr-x 1 xxxx xxxx 3345416 xxx xx xx:xx python* lrwxrwxrwx 1 xxxx xxxx 6 xxx xx xx:xx python2 -> python* lrwxrwxrwx 1 xxxx xxxx 6 xxx xx xx:xx python2.7 -> python* -rwxrwxr-x 1 xxxx xxxx 249 xxx xx xx:xx wheel*
To create a virtual environment for Python 3, use the -p python3
(or --python=python3
) flag, as follows:
$ virtualenv -p python3 myvenv3 New python executable in .../myvenv3/bin/python3 Also creating executable in .../myvenv3/bin/python Installing setuptools, pip, wheel...done. $ ll myvenv3/bin total 4408 -rw-rw-r-- 1 xxxx xxxx 2098 xxx xx xx:xx activate -rwxrwxr-x 1 xxxx xxxx 241 xxx xx xx:xx pip* -rwxrwxr-x 1 xxxx xxxx 241 xxx xx xx:xx pip3* -rwxrwxr-x 1 xxxx xxxx 241 xxx xx xx:xx pip3.5* lrwxrwxrwx 1 xxxx xxxx 7 xxx xx xx:xx python -> python3* -rwxrwxr-x 1 xxxx xxxx 4460336 xxx xx xx:xx python3* lrwxrwxrwx 1 xxxx xxxx 7 xxx xx xx:xx python3.5 -> python3* -rwxrwxr-x 1 xxxx xxxx 2357 xxx xx xx:xx python-config* -rwxrwxr-x 1 xxxx xxxx 248 xxx xx xx:xx wheel*
To activate/deactivate a virtual environment:
# Syntax: source [env-name]/bin/activate $ cd /path/to/project-directory $ source myvenv/bin/activate (myvenv)....$ # The command-prompt changes to "(venv)...$" # All Python tasks are carried out inside the virtual environment # with the specific version of Python interpreter and packages # To deactivate virtual environment (myvenv)....$ deactivate $ # The command-prompt returns to "$"
Take note that all commands issued under a virtual environment run under the Python of the virtual environment. Similarly, pip
issued under a virtual environment installs the package inside the virtual environment.
virtualenvwrapper
Reference: virtualenvwrapper documentation @ https://virtualenvwrapper.readthedocs.org/en/latest/.
"virtualenvwrapper
is a set of extensions to Ian Bicking’s virtualenv
tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies."
[TODO] more
REFERENCES & RESOURCES
- The Python's mother site @ www.python.org; "The Python Documentation" @ https://www.python.org/doc/; "The Python Tutorial" @ https://docs.python.org/tutorial/; "The Python Language Reference" @ https://docs.python.org/reference/.
- Eric Matthes, "Python Crash Course: A Hands-On, Project-Based Introduction to Programming", 3rd ed, 2023, No Starch Press.
- Vernon L. Ceder, "The Quick Python Book", 3rd ed, 2018, Manning (Good starting guide for experience programmers who wish to learning Python).
- Mark Lutz, "Learning Python", 5th ed, 2013; "Programming Python", 4th ed, 2011, O'reilly.
- David Beazley, Brian Jones, "Python Cookbook", 3rd ed, 2013, O'reilly.