# How to transit python code to julia

**URL:** https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583
**Category:** New to Julia
**Created:** [June 30, 2022, 8:51pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583 "2022-06-30T20:51:39Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ZiyiLiu](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@ZiyiLiu](https://discourse.julialang.org/u/ZiyiLiu)
#### Post date: [June 30, 2022, 8:51pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/1 "2022-06-30T20:51:39Z")

</div>

Hi,  
I have python codes for generate 3D tissue of plant, and the value of each point decides the color of the point(sphere). The values are from odes of the plant system.(So the python code is complex)

Do you know any way to do this?

Thanks!

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 30, 2022, 9:09pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/2 "2022-06-30T21:09:04Z")

</div>

Well, the first step is to check witch Python libraries you used and find equivalents in Julia (if needed, you do not need any replacement for Numpy, because array operations are part of Julia itself, or perhaps the standard library LinAlgebra).

Then start to translate the code…  
I usually create a .jl file and put all the Python code in commented form into that file and then start to translate piece by piece…

It helps if you have unit tests for your Python code which you should translate to Julia first. If you do not have unit tests you need to write them to ensure the results of your Python code are identical to the results of your Julia code.

At some point search-and-replace also becomes useful, e.g. replace `radians` with `deg2rad`, `**` with `^`, `np.zeros` with `zeros` etc…

When the code works correctly you can start to improve the performance by benchmarking your functions, trying to get rid of allocations, using concrete types for struct members etc.

In general a Python class translates to a Julia struct plus functions that have an instance of this struct as first parameter (instead of self).

---

<div class="post-metadata">

### Author: ![ZiyiLiu](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@ZiyiLiu](https://discourse.julialang.org/u/ZiyiLiu)
#### Post date: [June 30, 2022, 9:54pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/3 "2022-06-30T21:54:09Z")

</div>

Thanks! I am trying to use pycall in julia. However, when I use ‘plt = pyimport(“matplotlib.pyplot”)’, it showed the error:  
"The Python package matplotlib.pyplot could not be imported by pyimport. Usually this means  
that you did not install matplotlib.pyplot in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

D:\anaconda\python.exe

and you should use whatever mechanism you usually use (apt-get, pip, conda,  
etcetera) to install the Python package containing the matplotlib.pyplot module."

I installed matplotlib.pyplot later via ‘conda install matplotlib’, but it still shows the problem. Could you help me with this? THANKS!

---

<div class="post-metadata">

### Author: ![garrek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrek/32/27937_2.png) [@garrek](https://discourse.julialang.org/u/garrek)
#### Post date: [July 1, 2022, 1:17am UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/4 "2022-07-01T01:17:57Z")

</div>

If your goal is to translate your Python code to Julia, I would not recommend trying to call your Python code in Julia. This is just adding an extra step. I would only use PyCall if you don’t intend on translating some code but you want to integrate it into your Julia code or something.

I would run your Python code in your usual Python editor for reference and translate your code into Julia just as @ufechner7 suggests. Work step by step and make each small part work correctly.

I also came from Python and moved an entire electrodynamics simulation to Julia. It’s much easier to work directly in Julia or directly in Python because then you won’t confuse the two languages as much. In fact, my Julia code looks different from my Python code because l took advantage of the unique features of Julia.

As you translate your code, you can ask specific questions about how to do something in Julia.  
I’m sure very few people know how to generate 3d tissue of a plant (in any language!), so breaking your project into smaller pieces and asking more specific questions as you go will be more helpful to you, and more people will be able to help you. 😉

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 1, 2022, 4:33pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/5 "2022-07-01T16:33:27Z")

</div>

If you are installing PyCall and PyPlot for the first time, just do `ENV["PYTHON"]=""` before running `Pkg.add("PyPlot")`. Otherwise, you can reconfigure PyCall to use Conda via:

```julia
ENV["PYTHON"]=""
Pkg.build("PyCall")

```

The next time you import `PyPlot`, it will tell Conda to install Matplotlib.

---

<div class="post-metadata">

### Author: ![ZiyiLiu](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@ZiyiLiu](https://discourse.julialang.org/u/ZiyiLiu)
#### Post date: [July 2, 2022, 5:39pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/6 "2022-07-02T17:39:12Z")

</div>

Thanks!  
I returned to the python and had the 3D tissue done using the Mayavi.

For translate python code to julia, I have one main problem, that is how to translate tellurium in python to julia, do you know how to do it?

---

<div class="post-metadata">

### Author: ![ZiyiLiu](https://avatars.discourse-cdn.com/v4/letter/z/7993a0/32.png) [@ZiyiLiu](https://discourse.julialang.org/u/ZiyiLiu)
#### Post date: [July 2, 2022, 5:39pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/7 "2022-07-02T17:39:33Z")

</div>

Thanks! I got this!

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [July 2, 2022, 6:54pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/8 "2022-07-02T18:54:26Z")

</div>

See the post by [tamasgal](https://discourse.julialang.org/u/tamasgal)

> [@Workflow for converting Python scripts](https://discourse.julialang.org/t/workflow-for-converting-python-scripts/32737/3):
>
> My recipe for converting from one language to another is quite simple, at least when the second language is able to fully wrap the first one, which is the case for Julia thanks to PyCall. This is what I usually do when I go from Python -\> Julia: Create a script/workflow which you want to port to Julia, this can be something easy like calling a function, or more complex, like full analysis chain. Smaller steps are easier though. Write unit tests and/or high level tests in Julia to have an autom…

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 2, 2022, 9:07pm UTC](https://discourse.julialang.org/t/how-to-transit-python-code-to-julia/83583/9 "2022-07-02T21:07:34Z")

</div>

> [@ZiyiLiu](#):
>
> For translate python code to julia, I have one main problem, that is how to translate tellurium in python to julia, do you know how to do it?

As far as I see Tellurium is a Python library for simulating biological systems. There are a lot of libraries in Julia you could use for this task, so you have to be more specific what exactly you want to simulate…
