# Importing Python Modules

**URL:** <https://discourse.julialang.org/t/importing-python-modules/105123>\
**Category:** New to Julia\
**Tags:** python\
**Created:** [October 18, 2023, 12:43pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123 "2023-10-18T12:43:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hao\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hao_wang/32/202555_2.png) [@Hao\_Wang](https://discourse.julialang.org/u/Hao_Wang)\
**Post date:** [October 18, 2023, 12:43pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/1 "2023-10-18T12:43:14Z")

</div>

Hello mates! This is a new Julia user. I was caught by a trouble to import a Py module “graphviz” to Julia.

I’ve checked that I am referring to the correct path of Py

```julia
ENV["PYTHON"]="/Users/whao/anaconda3/bin/python3.11"

```

and I’ve added my target module by `Conda`

```julia
using Conda
Conda.add("graphviz")

```

and I confirm in the library folder that it does exist in

```julia
"/Users/whao/anaconda3/lib/graphviz"

```

But once I import the module

```julia
using PyCall
pyimport("graphviz")

```

it comes up with error

> ERROR: PyError (PyImport\_ImportModule
> 
> The Python package graphviz could not be imported by pyimport. Usually this means  
> that you did not install graphviz in the Python version being used by PyCall.
> 
> PyCall is currently configured to use the Python version at:
> 
> /Users/whao/anaconda3/bin/python3.11
> 
> and you should use whatever mechanism you usually use (apt-get, pip, conda,  
> etcetera) to install the Python package containing the graphviz module.
> 
> One alternative is to re-configure PyCall to use a different Python  
> version on your system: set ENV[“PYTHON”] to the path/name of the python  
> executable you want to use, run Pkg.build(“PyCall”), and re-launch Julia.
> 
> Another alternative is to configure PyCall to use a Julia-specific Python  
> distribution via the Conda.jl package (which installs a private Anaconda  
> Python distribution), which has the advantage that packages can be installed  
> and kept up-to-date via Julia. As explained in the PyCall documentation,  
> set ENV[“PYTHON”]=“”, run Pkg.build(“PyCall”), and re-launch Julia. Then,  
> To install the graphviz module, you can use `pyimport_conda("graphviz", PKG)`,  
> where PKG is the Anaconda package that contains the module graphviz,  
> or alternatively you can use the Conda package directly (via  
> `using Conda` followed by `Conda.add` etcetera).
> 
> ) \<class ‘ModuleNotFoundError’\>  
> ModuleNotFoundError(“No module named ‘graphviz’”)
> 
> Stacktrace:  
> [1] pyimport(name::String)  
> @ PyCall ~/.julia/packages/PyCall/ilqDX/src/PyCall.jl:558  
> [2] top-level scope  
> @ REPL[2]:1

It says the module is not found. What’s the problem?

Cheers!

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 18, 2023, 12:54pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/2 "2023-10-18T12:54:33Z")

</div>

It might be simpler for you to look into PythonCall.jl, which is newer. It downloads Python for your if needed, and takes care of dependencies using Conda (actually compatible micromamba clone).

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [October 18, 2023, 12:56pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/3 "2023-10-18T12:56:22Z")

</div>

Welcome!  
Not a direct answer to your question - but there is a Julia wrapper around [GraphViz](https://github.com/JuliaGraphs/GraphViz.jl), you can install it from the REPL by typing `]` to enter the package manager, and then type `add GraphViz` to add it. You type the backspace key on an empty line in the package manager to get out.

You may find the Julia-native library to be a lot more stable in your code.

In general, you may find that simply doing a web search for a library will wind up giving you the name of a Julia package for it, the community has a very diverse set of add-on packages.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 18, 2023, 1:15pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/4 "2023-10-18T13:15:18Z")

</div>

> [@Hao\_Wang](#):
>
> and I’ve added my target module by `Conda`
> 
> ```julia
> using Conda
> Conda.add("graphviz")
> 
> ```

Note that by default, the `Conda` package installs its _own_ conda distribution, independent of the one you’ve apparently installed into `/Users/whao/anaconda3`, unless you configured Conda.jl explicitly to use that environment.

But more importantly, **you are installing the wrong package**.

> I confirm in the library folder that it does exist in `/Users/whao/anaconda3/lib/graphviz`

That only confirms that the **C library** is installed, not the Python module. As [explained in this Stackoverflow question](https://stackoverflow.com/questions/33433274/anaconda-graphviz-cant-import-after-installation), the `graphviz` anaconda package is only the C library and executable, _not_ the Python wrapper module.

If you want the Python wrapper module then you need to install `python-graphviz` (anaconda) or `pygraphviz` (conda-forge).

---

<div class="post-metadata">

**Author:** ![Hao\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hao_wang/32/202555_2.png) [@Hao\_Wang](https://discourse.julialang.org/u/Hao_Wang)\
**Post date:** [October 18, 2023, 1:35pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/5 "2023-10-18T13:35:16Z")

</div>

Thank you Palli, I’ve settled it!

---

<div class="post-metadata">

**Author:** ![Hao\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hao_wang/32/202555_2.png) [@Hao\_Wang](https://discourse.julialang.org/u/Hao_Wang)\
**Post date:** [October 18, 2023, 1:35pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/6 "2023-10-18T13:35:48Z")

</div>

Thank you frylock, I gotta understand more about that!

---

<div class="post-metadata">

**Author:** ![Hao\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hao_wang/32/202555_2.png) [@Hao\_Wang](https://discourse.julialang.org/u/Hao_Wang)\
**Post date:** [October 18, 2023, 1:36pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/7 "2023-10-18T13:36:25Z")

</div>

Thank you stevengj, I follow the SF comment and solve it.

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [October 18, 2023, 1:46pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/8 "2023-10-18T13:46:11Z")

</div>

Also, depending on what you are doing with GraphViz, the file format is actually quite readable. The API is nice to have in Python, Julia, whatever … but GraphViz is even a great command-line tool.

You can try it out [online](https://dreampuf.github.io/GraphvizOnline/) without even installing the package and there are [lots of examples](https://graphviz.org/documentation/) of how to write input files (`*.dot` files). Once you get familiar with all of the neat options, you can work some real magic with it in Julia.

---

<div class="post-metadata">

**Author:** ![Hao\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hao_wang/32/202555_2.png) [@Hao\_Wang](https://discourse.julialang.org/u/Hao_Wang)\
**Post date:** [November 5, 2023, 9:33pm UTC](https://discourse.julialang.org/t/importing-python-modules/105123/9 "2023-11-05T21:33:51Z")

</div>

Cool, I’ve got the point, thanks!
