Most Python programming books cover the basics of python programming but don’t always deal very much with practical implementation and usage.
As someone who is relatively new to python I come across lot’s of things that are probably obvious for an experienced programmer but not so obvious when you are just starting out.
These are my Notes that I’ve made while creating basic Python programs that may be of use. They cover:
- Installing Python windows and Linux
- Multiple Python versions
- PIP-Python Package Manager
- Python file and directory structure
- Python Modules
- Running programs
The python crash course resources has instructions for installing Python on windows, Linux and MAC OS.
Multiple Python Versions
You can have multiple python versions installed on the same machine, but it can cause problems if you aren’t very experienced.
One of the common problems I encountered was when I installed a package with PIP it would be installed to the wrong version.
You can use the command
To see where pip will install packages
To see where pip has installed a package type:
pip show package_name
PIP – The Python Package Manager
PIP is a package manger used to install Python modules and packages.
PIP normally comes installed with Python.
If it isn’t already installed you can install it by following these instructions.
Modules and packages like the MQTT client are installed to the Lib\site-packages folder.
Common pip commands
- pip install package-name
- pip uninstall package-name
- pip install -U pip #upgrade pip
- python -m pip install -U pip #upgrade pip on windows.
On Linux if you run as sudo and pip3 then the packages are installed in the /usr/local/lib/python3.5/dist-packages folder.
In this case python3.5 is the latest version.
If you don’t run as sudo then they installed in the home folder under .local/lib/python3.5/dist-packages
Linux Folders and Python
- usr/bin has binaries and links from generic e.g 2 to 2.7 etc
- usr/lib -main python files and also the idle
- /usr/local/lib/ – distribution packages that you have installed. E.g the paho mqtt client would be installed there.
A python package is a collection of python modules. It is effectively a folder containing python files.
A python package directory has to have a __init__.py file.
The presence of this file indicates to the Python interpreter that the directory/folder contains python modules.
Although the __init__.py file can contain configuration information is is also often completely blank.
See this article be Pythonic:__init__.py for more details.
A python module is a file containing python code. a module can define classes,functions and variables.
A module can be imported into another Python program or run directly.
Namespaces and Modules
A module also defines a namespace. Two modules can contain functions and variable with the same name as they are effectively in two separate namespaces.
Example two modules, m1 and m2 each contain a function add().
To access the add function from modules m1 you need to use the following syntax:
To access the add function from modules m2 you need to use the following syntax:
When using the import module format then the module is imported with its namespace and so to access a function in that module you will need to prefix it with the module name.
If you use the from module import function format then you can access the function without prefixing it with the module name. Ref Dive into Python
The __main__ module
When you run a script by typing:
The Python interpreter runs it as module called __main__ which gets its own namespace.
If you imported the script then it would have the module name myscript.
You will also encounter the following code:
if __name__ == "__main__": # execute only if run as a script main()
This is found in python modules that can be imported or executed directly. These articles give a reasonable explanation
Where are my Packages being installed?
On Windows the packages are placed in the Lib/site-packages directory which is under the root of the python install. That is if you install python to python34 then new packages installed with pip will be placed in the python34\Lib\site-packages folder.
On linux the packages seem to be installed either in one of these locations:
You can find out where Python expects the packages to be found using the following simple script
import site print (site.getsitepackages())
You can also do it from the python command prompt as follows:
From the command line on Windows or linux use:
If you have several versions on Python available e.g. python 2 and 3, then you can use
to start your program using the Python3 Interpreter.
To find out which version of python is the default version use:
If Your script needs a particular version to work then use:
import sys assert sys_version_info >=(2,5)
The SheBang Line
At the top of scripts written for Linux you often see this line.
This lets you run your program without starting the interpreter so you use simply:
However the above may not work if you have multiple python versions installed as it will use the default version.
In this case you will need to be explicit and use
To run using python3.5
On Windows you can also add a Shebang line:
Will run your program with the python3.4 interpreter.
Here is a good overview of the shebang line and the env program (note refers to node but same for python).
Exiting Python Scripts
Do you use quit,exit,sys,exit etc? Good explanation here.
Seems best to use:
The above covers the main areas where I have had difficulty. You’re experience will be different you can help me improve the usefulness of this page by letting me know any areas that have caused problems by using the contact page or the comments form below.