PythonHowTo
Sommaire
Where to start
Virtual Environments for Python Libraries
The new recommended way to install Python modules (libraries) is to use virtual environments to avoid mixing your installed packages with the system Python:
$ python -m venv <directory-name> $ source <directory-name>/bin/activate
Once one wants to exit the Python environment, one should run the deactivate command:
$ deactivate
There is otherwise a more recent and very interesting tool called uv to manage Python environments.
On the group Linux computers, the libraries installed on the ~nicolas account are accessible to avoid duplication, in particular for the deep learning ultralytics library.
Install Modules
Ref https://docs.python.org/3/installing/index.html
Once you have a virtual environment, activate and install the desired modules:
$ pip install SomePackage
or
$ python -m pip install SomePackage
Old Material
- It is easy to install the necessary packages using easy_install/pip/conda once you have the basics
- pip is recommended with the online script https://bootstrap.pypa.io/get-pip.py. Any module can be installed through and updated: pip install <module> --upgrade (will install in the user home directory).
- I recommend a scientific distribution of Python, ie Python with the improved interpreter iPython and the core scientific libraries (Numpy, Scipy, Matplotlib, Pandas, etc.) that are used for example in the Traffic Intelligence project:
- There are three main choices: anaconda, PythonXY and Enthought Python Distribution.
- In addition, if you want to install other Python packages not provided by default, such as OpenCV that is needed to display video data and replay extracted trajectories over the video (used in the cvutils module). For example:
$ conda install opencv3 $ pip install opencv-python
or (you can search your preferred version using)
$ anaconda search -t conda opencv $ anaconda show <user/package> $ conda install --channel https://conda.anaconda.org/menpo opencv3 # for example
I recommend a good text editor with indispensable functionalities such as text coloring and parenthesis highlighting such as notepad++ on Windows, or atom and gedit (for Windows, Mac and Linux).
Environment Variables for your Code and Other Downloaded Libraries
To use Python modules from your code or downloaded code, the Python interpreter needs to know where to look for them. The first and preferred way is to set (or add to) the environment variable PYTHONPATH to refer to the location of the Python modules to load:
- on Windows, add or modify the PYTHONPATH environment variable. Given the example installation path C:\Code\my_python_modules\, it should be set to C:\Code\my_python_modules\, e.g. C:\Users\username\traffic-intelligence\ for the Traffic Intelligence modules (in the trafficintelligence package);
- on Linux, add or modify the PYTHONPATH environment variable by typing $export PYTHONPATH=/home/username/Code/my_python_modules/ in a terminal. However, you have to do that for each terminal before running a Python interpreter. The better way is to set the environment variable when your shell starts. If using bash, simply add the previous command at the end of the user .bashrc file (in the home directory);
- on both platforms, other paths can be added, separated by :.
If that did not work, you can add the path to the sys.path system variable in the Python interpreter or at the beginning of your code, before your import statements:
$ import sys $ sys.append('path_to_python_modules') $ import your_module
To test your installation, start a Python interpreter (or even better, ipython) in a new terminal (the variable will not be added to the ones that were started before the PYTHONPATH variable was added) and try to import one of your modules in the directory referred to by PYTHONPATH:
$ import your_module
Tools and Scientific libraries
Tools
- Better interpreter: ipython https://ipython.org/
- Running and presenting code with Jupyter Notebooks https://jupyter.org/
Libraries
- Matplotlib http://matplotlib.org
- Numpy http://www.numpy.org
- Computer Vision: OpenCV , scikit-image
- Statistics and machine learning: scipy, scikit-learn
- Geometry and GIS: shapely
- Tabular data loading/processing: pandas
- ORM: sqlalchemy
Links
- Finding the shortest cycling path in the shade https://medium.com/@tanyamarleytsui/shady-streets-6dad0979c13a
Random Topics
Shapely
- I left functions in moving (in Point) that returns whether a point is in a polygon or not. inPolygonNoShapely(polygon) where polygon is a Nx2 numpy array representing the polygon
- with Shapely, use their polygon and point class, eg
from shapely.geometry import Polygon, Point poly = Polygon(array([[0,0],[0,1],[1,1],[1,0]])) p = Point(0.5,0.5) poly.contains(p) -> returns True poly.contains(Point(-1,-1)) -> returns False
You can convert a moving.Point to a shapely point: p = moving.Point(1,2) p.asShapely() returns the equivalent shapely point
If you have several points to test, use moving.pointsInPolygon(points, polygon) where points are moving.Point and polygon is a shapely polygon.
You should dig in shapely for functions that compute intersections (there is one in our library, but it is slow): http://toblerity.github.io/shapely/manual.html#object.crosses from shapely.geometry import Polygon, Point, LineString coords = [(0, 0), (1, 1), (1, -1), (0, 1)] LineString(coords).crosses(LineString([(0, 1), (1, 0)]))