PythonHowTo : Différence entre versions

De Transport
(Where to start)
(Where to start)
 
(5 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
 
= Where to start =
 
= Where to start =
 
[[Programming_Resources#Python|Python tutorials]]
 
[[Programming_Resources#Python|Python tutorials]]
 +
== Virtual Environments for Python Libraries ==
  
== Install Python and Modules ==
+
The new recommended way to install Python modules (libraries) is to use virtual environments to avoid mixing your installed packages with the system Python:
  
* 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 project [https://bitbucket.org/Nicolas/trafficintelligence/ Traffic Intelligence]:
+
$ python -m venv <directory-name>
** There are three main choices: [https://www.continuum.io/why-anaconda anaconda], [https://python-xy.github.io/ PythonXY] and [http://www.enthought.com/products/epd.php Enthought Python Distribution].  
+
$ source <directory-name>/bin/activate
** 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). If using anaconda, OpenCV (it it similar for other packages) can be installed in the following way (typing the following in a command line):
+
 
 +
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 [https://docs.astral.sh/uv/ uv] to manage Python environments.
 +
 
 +
On the group Linux computers, the libraries installed on the <tt>~nicolas</tt> 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 <tt>module</tt> can be installed through and updated: <tt>pip install <module> --upgrade</tt> (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 [https://trafficintelligence.confins.net Traffic Intelligence] project:
 +
** There are three main choices: [https://www.continuum.io/why-anaconda anaconda], [https://python-xy.github.io/ PythonXY] and [https://assets.enthought.com/downloads/ 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
 
  $ conda install opencv3
 +
$ pip install opencv-python
  
 
or (you can search your preferred version using)
 
or (you can search your preferred version using)
Ligne 15 : Ligne 45 :
 
  $ anaconda show <user/package>
 
  $ anaconda show <user/package>
 
  $ conda install --channel https://conda.anaconda.org/menpo opencv3 # for example
 
  $ conda install --channel https://conda.anaconda.org/menpo opencv3 # for example
 
* On Linux, it is easy to install the necessary packages from the repositories or though easy_install/pip/conda once you have the basics (and python-dev)
 
** pip is recommended on Linux with the online script https://bootstrap.pypa.io/get-pip.py. Any <tt>module</tt> can be installed through and updated: <tt>sudo pip install <module> --upgrade</tt>.
 
* On Windows, you can manually get the necessary scientific packages from this [http://www.lfd.uci.edu/~gohlke/pythonlibs/ Unofficial Windows Binaries for Python Extension Packages]
 
  
 
I recommend a good text editor with indispensable functionalities such as text coloring and parenthesis highlighting such as [http://notepad-plus-plus.org notepad++] on Windows, or [https://atom.io/ atom] and [http://projects.gnome.org/gedit/ gedit] (for Windows, Mac and Linux).
 
I recommend a good text editor with indispensable functionalities such as text coloring and parenthesis highlighting such as [http://notepad-plus-plus.org notepad++] on Windows, or [https://atom.io/ atom] and [http://projects.gnome.org/gedit/ gedit] (for Windows, Mac and Linux).
Ligne 40 : Ligne 66 :
 
  $ import your_module
 
  $ import your_module
  
== Recommended Tools ==  
+
== Tools and Scientific libraries ==  
  
 +
Tools
 
* Better interpreter: ipython https://ipython.org/
 
* Better interpreter: ipython https://ipython.org/
 
* Running and presenting code with Jupyter Notebooks https://jupyter.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 =
 
= Links =

Version actuelle en date du 28 mars 2025 à 17:11

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

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

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