# How to import Python Libraries with ExternalEvaluate?

**URL:** <https://community.wolfram.com/t/how-to-import-python-libraries-with-externalevaluate/21356>\
**Category:** Questions\
**Tags:** external-interfaces\
**Created:** [January 5, 2023, 10:48pm UTC](https://community.wolfram.com/t/how-to-import-python-libraries-with-externalevaluate/21356 "2023-01-05T22:48:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maa48](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@maa48](https://community.wolfram.com/u/maa48)\
**Post date:** [January 5, 2023, 10:48pm UTC](https://community.wolfram.com/t/how-to-import-python-libraries-with-externalevaluate/21356/1 "2023-01-05T22:48:11Z")

</div>

I want to use specific python libraries via the Mathematica frontend. This is possible as follows

```
In[30]:= ExternalEvaluate["Python", "from sympy import isprime; isprime(7)"]
Out[30]= True

```

However, if I directly try to evaluate the same using \> at the beginning of a line to use an external code cell, this does not work. For instance,

> from sympy import isprime; isprime(7)

gives as output,

```
Out[31] = Failure[[WarningSign] Message: No module named 'sympy' Tag: PythonError ]

```

How can we make this work from within external code cells?

---

<div class="post-metadata">

**Author:** ![alec](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/alec/32/9989_2.png) [@alec](https://community.wolfram.com/u/alec)\
**Post date:** [January 6, 2023, 12:32am UTC](https://community.wolfram.com/t/how-to-import-python-libraries-with-externalevaluate/21356/2 "2023-01-06T00:32:40Z")

</div>

You should be able to use [StartExternalSession](https://www.wolfram.com/language/12/external-system-integration/use-external-language-sessions.html?product=mathematica#:~:text=Use%20External%20Language%20Sessions%20External%20language%20sessions%20can,image-processing%20code%20to%20intermingle%20and%20create%20unique%20results.) to create an ongoing Python session that you can import things into before doing your main ExternalEvaluate calls.

This should also make the repeated calls a bit faster, so that is a plus.

---

<div class="post-metadata">

**Author:** ![maa48](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@maa48](https://community.wolfram.com/u/maa48)\
**Post date:** [January 6, 2023, 2:11am UTC](https://community.wolfram.com/t/how-to-import-python-libraries-with-externalevaluate/21356/3 "2023-01-06T02:11:57Z")

</div>

Thanks. This was solved by unregistering one of the python distributions that I had on the system. The two methods were accessing different distributions.

---

<div class="post-metadata">

**Author:** ![arnoudb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arnoudb/32/41177_2.png) [@arnoudb](https://community.wolfram.com/u/arnoudb)\
**Post date:** [April 17, 2025, 3:46pm UTC](https://community.wolfram.com/t/how-to-import-python-libraries-with-externalevaluate/21356/4 "2025-04-17T15:46:18Z")

</div>

Recent versions of Wolfram Language support Python virtual environments that let you specify Python packages quite easily. For example, this creates a session with a Python virtual environment that includes the `sympy` package:

```
session = StartExternalSession[<|
   "System" -> "Python",
   "Evaluator" -> <|"Dependencies" -> {"sympy"}|>,
   "SessionProlog" -> "from sympy import symbols, Eq, solve"|>]

```

You can then use ExternalEvaluate and call Python code, for example:

```
ExternalEvaluate[session, "
x, y = symbols('x y')
equation = Eq(x**5 - 4, 0)
[complex(s) for s in solve(equation, x)]"]

```

The output will be a list of complex numbers:

```
{1.31951 + 0. I, 0.40775 - 1.25493 I, 0.40775 + 1.25493 I, -1.0675 - 0.775587 I, -1.0675 + 0.775587 I}

```

Of course you can solve this specific example directly in the Wolfram Language as well and much more elegantly:

```
SolveValues[x^5 == 4.0, x]

```

But if you have existing Python `sympy` code that you would like to integrate directly into the Wolfram Language, then this is the best way to do it. I have attached a notebook with the code from this comment.

[sympy-01.nb](https://community.wolfram.com/uploads/short-url/vh9kQTKDMOoP8852hi1sV8dTodX.nb) (48.8 KB)
