
How to run Python from Julia
Shuvomoy Das Gupta
March 31, 2025
In this blog, we will discuss how to install and run Python from Julia using the PyCall package.
Install PyCall and Conda
We will install PyCall first for running Python code within Julia and the Conda for installing Python modules. Pycall is available at https://github.com/
JuliaPy/PyCall.jl, and Conda is available at
https://github.com/JuliaPy/Conda.jl.
1
2 # Install PyCall and Conda
3
4 using Pkg
5
6 Pkg.add("PyCall")
7
8 Pkg.add("Conda")
9
10 # Let us now install a Python module from Julia
11
12 Conda.add("numpy")
13
A first example
Consider the following code of running a Python function from Julia:
1
2 using PyCall, Conda
3
4
5 py"""
6
7 import numpy as np
8
9 def calculate_stats(numbers):
10
11 # Calculates the mean and standard deviation of a list of numbers using NumPy.
12 # Args:
13 # ====
14 # numbers: A list or NumPy array of numerical values.
15 # Returns:
16 # =======
17 # A tuple containing the mean and standard deviation.
18
19 np_array = np.array(numbers)
20 mean = np.mean(np_array)
21 std_dev = np.std(np_array)
22 return mean, std_dev
23
24 """
25
26 # Generate sample data and run the Python code from Julia
27
28 data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
29
30 mean_value, std_dev_value = py"calculate_stats"(data)
31
32 # will get output (5.5, 2.8722813232690143)
33
34
Explanation of the code
1. using PyCall, Conda
This line imports two Julia packages:
• PyCall: A package that enables Julia to call Python code seamlessly. It provides an interface to execute Python functions
and access Python objects from Julia.
• Conda: A package manager used by PyCall to manage Python dependencies (e.g., installing numpy if not already present in
the Python environment).
Together, these ensure that Python and its libraries are available within Julia.
2. py""" Block
The py""" syntax defines a Python code block within Julia. Everything between the triple quotes is executed as Python
code when the block is evaluated. Here, it:
• Imports the Python library numpy as np, a popular library for numerical computations.
• Defines a function calculate_stats(numbers) that takes a single argument numbers, which is expected to be a list or
array of numerical values.
3. calculate_stats Function
The Python function performs the following steps:
• np_array = np.array(numbers): Converts the input numbers into a NumPy array, ensuring compatibility with NumPy
functions.
• mean = np.mean(np_array): Computes the arithmetic mean of the array.
• std_dev = np.std(np_array): Computes the sample standard deviation.
• return mean, std_dev: Returns a tuple containing the mean and standard deviation.
4. data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
This line defines a Julia array data containing the integers from 1 to 10. In Julia, arrays are 1-based, and this is a simple list
of 𝑛 = 10 elements.
5. mean_value, std_dev_value = py"calculate_stats"(data)
This line:
• Calls the Python function calculate_stats defined earlier, passing the Julia array data as an argument.
• Uses py"calculate_stats" syntax from PyCall to invoke the Python function directly.
• Automatically converts the Julia array data to a Python-compatible list or array, computes the results in Python, and
converts the returned tuple back to Julia variables.
• Assigns the returned mean to mean_value and the standard deviation to std_dev_value.
Run an entire Python script in PyCall
Suppose we have a Python script called test.py. We can execute @pyinclude("test.py"), which is equivalent to pasting all
the content of the script into a py"""...""" string.