Macaulay2 » Documentation
Packages » Python » Python tutorial: creating a virtual environment and installing NumPy
next | previous | forward | backward | up | index | toc

Python tutorial: creating a virtual environment and installing NumPy

In this tutorial, we demonstrate how to create a Python virtual environment and install the numpy module within it. A virtual environment allows us to isolate Python packages from the system-wide installation, ensuring that dependencies do not interfere with other projects.

Step 1: Create a Virtual Environment

First, we choose a directory where we want to create our virtual environment. We then call setupVirtualEnvironment with that directory. This command initializes a new virtual environment, which includes a dedicated Python interpreter and an isolated package installation directory.

i1 : needsPackage "Python";
i2 : dir = applicationDirectory() | "venv"

o2 = /home/m2user/.Macaulay2/venv
i3 : setupVirtualEnvironment dir

Step 2: Reload the Python Package with the Virtual Environment

Next we restart Macaulay2 so we can initialize the Python binary in the new virtual environment.

i4 : restart

To ensure that the Python interface uses the newly created virtual environment, we load the Python package while specifying the virtual environment’s Python interpreter. This is done by setting the "executable" configuration option to point to the python3 binary inside our virtual environment.

i1 : loadPackage("Python", Configuration => {
         "executable" => applicationDirectory() | "venv/bin/python3"})

o1 = Python

o1 : Package

At this point, all Python commands will use the virtual environment's interpreter rather than the system-wide Python installation.

Step 3: Install NumPy

Next, we install the NumPy module using pipInstall. This downloads and installs NumPy into the virtual environment.

i2 : pipInstall "numpy"
Collecting numpy
  Downloading numpy-2.2.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.0/62.0 kB 2.5 MB/s eta 0:00:00

Downloading numpy-2.2.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.1 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.1/16.1 MB 47.9 MB/s eta 0:00:00

Installing collected packages: numpy
Successfully installed numpy-2.2.4

Once this step completes, NumPy is fully installed and ready for use.

Step 4: Import and Use NumPy

Now that NumPy is installed, we can call installNumPyMethods and begin using it. Let's verify that it works by multiplying two matrices.

i3 : installNumPyMethods()

o3 = <module 'numpy' from '/home/profzoom/.Macaulay2/venv/lib/python3.10/site-
     packages/numpy/__init__.py'>

o3 : PythonObject of class module
i4 : A = toPython matrix {{1, 2}, {3, 4}}

o4 = [[1 2]
      [3 4]]

o4 : PythonObject of class numpy.ndarray
i5 : B = toPython matrix {{5, 6}, {7, 8}}

o5 = [[5 6]
      [7 8]]

o5 : PythonObject of class numpy.ndarray
i6 : A @ B

o6 = [[19 22]
      [43 50]]

o6 : PythonObject of class numpy.ndarray

See also


The source of this document is in Python/doc/tutorials.m2:132:0.