PythonHowTo

De Transport
Révision de 17 juillet 2026 à 07:50 par NicolasSaunier (discuter | contributions) (Old Material)

Where to start

Python tutorials

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

I highly recommend the IPython interpreter and, to a lesser degree, jupyter (for the notebooks). See Traffic Intelligence requirements.txt for the recommended scientific packages (pip install -r requirements.txt).

Old Material

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

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

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)]))