# Issue with ModelingToolkit in Pluto

**URL:** <https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517>\
**Category:** Pluto\
**Created:** [May 4, 2021, 1:05pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517 "2021-05-04T13:05:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [May 4, 2021, 1:05pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/1 "2021-05-04T13:05:00Z")

</div>

I get the following error for initialising variables. the code runs fine in the terminal and in vscode using the same Julia instance.

 ![Screenshot 2021-05-04 at 15.02.53](https://global.discourse-cdn.com/julialang/original/3X/b/8/b86a64c4f468d53b443a5f3da38bc669eb6a4a7c.png)

Any ideas what this could be?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 4, 2021, 2:49pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/2 "2021-05-04T14:49:33Z")

</div>

I don’t know. I’ll need @fonsp to help. It should work in any standard REPL or IDE, so Pluto must be changing the semantics of the global scope a bit.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [May 4, 2021, 3:15pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/3 "2021-05-04T15:15:11Z")

</div>

Is this related to the lack of the interactive scoping of Pluto? Ie it is not consistent with the REPL/vscode inline/jupyter.

---

<div class="post-metadata">

**Author:** ![Pangoraw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pangoraw/32/24719_2.png) [@Pangoraw](https://discourse.julialang.org/u/Pangoraw)\
**Post date:** [May 4, 2021, 3:29pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/4 "2021-05-04T15:29:49Z")

</div>

This is due to the way Pluto handles running new cells. I think your issue is similar to [Not escaping in macro definition gives confusing error · Issue #1112 · fonsp/Pluto.jl · GitHub](https://github.com/fonsp/Pluto.jl/issues/1112)

Note: I am working on adding support for Macros inside Pluto, if you want to try it see [https://github.com/fonsp/Pluto.jl/pull/1032](https://github.com/fonsp/Pluto.jl/pull/1032)

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [May 4, 2021, 3:30pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/5 "2021-05-04T15:30:07Z")

</div>

It will probably work if you don’t use the macros. Pluto can’t see the assignments that are accomplished by the macros.

EDIT: See [https://github.com/fonsp/Pluto.jl/issues/196](https://github.com/fonsp/Pluto.jl/issues/196). It may be worth making a specific new issue to see if someone can do some special case code for these macros.

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [May 4, 2021, 3:33pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/6 "2021-05-04T15:33:24Z")

</div>

Ok, this makes sense. Can I define the variables without the macro and still use the ModellingToolkit features?

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [May 4, 2021, 3:44pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/7 "2021-05-04T15:44:14Z")

</div>

Also, the code above works in Neptune (non-reactive version of Pluto). Good to know for these special cases. I am very fond of working in Pluto…

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [May 4, 2021, 3:48pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/8 "2021-05-04T15:48:06Z")

</div>

I think it should work and I think the MTK docs have examples without macros (but they are a lot wordier). Pluto also can’t track mutation, so if you make an object and then mutate it, you probably want to do both of those operations in one cell. A task in which the mutation can’t be contained in that way probably isn’t a fit for Pluto.

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [May 4, 2021, 3:58pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/9 "2021-05-04T15:58:07Z")

</div>

yes, actually just doing

```julia
begin 
	@parameters t σ ρ β
	@variables x(t) y(t) z(t)	
end

```

makes it work in Pluto. Nice!

But also, does anyone know how to get the Latex rendered?

 ![Screenshot 2021-05-04 at 17.58.35](https://global.discourse-cdn.com/julialang/original/3X/e/8/e899591a4911ef6c91ec129b91374c5b35318fe7.png)

---

<div class="post-metadata">

**Author:** ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)\
**Post date:** [May 4, 2021, 8:14pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/10 "2021-05-04T20:14:04Z")

</div>

This solution worked for me:  
[https://www.reddit.com/r/Julia/comments/mu9far/pluto\_doesnt\_render\_symbolicsjl\_expressions/](https://www.reddit.com/r/Julia/comments/mu9far/pluto_doesnt_render_symbolicsjl_expressions/)

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [May 4, 2021, 8:23pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/11 "2021-05-04T20:23:09Z")

</div>

nice, thanks!

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 18, 2021, 8:39pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/12 "2021-08-18T20:39:50Z")

</div>

Hi all,

I am setting a simple optimization model using JuMP. The model is as simple as this:

```julia
begin
	m1 = Model(Ipopt.Optimizer)
	@variable(m1, z1)
	@variable(m1, z2)
	@variable(m1, z3)

	@objective(m1, Min, z1^2 + 0.5*z2^2 + 0.4*z3^2)

	@constraint(m1, cons1b, 0.5*z1 + z2 == 10)
	@constraint(m1, cons1c, 0.5*z1 + z3 == 20)
end

```

I can solve it in VScode, Atom, and Jupyter as well. In Pluto the following error pops up:

```julia
cannot assign a value to variable workspace5.z1 from module workspace11

	1. macro expansion @ macros.jl:149[inlined]
	2. top-level scope @ Local: 3

```

Is there a way around this problem? I have been migrating a significant amount of notebooks into Pluto, and they have to be ready in early September. I realize that the problem is related to the macros used in JuMP, but JuMP does not have an alternative syntax.

Help will be very much appreciated. Thanks.

 ![a](https://global.discourse-cdn.com/julialang/original/3X/5/2/52ed6a29e2281b4a7560751f656ecab792704141.png)

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 18, 2021, 9:00pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/13 "2021-08-18T21:00:24Z")

</div>

> I have been migrating a significant amount of notebooks into Pluto, and they have to be ready in early September.

I would not recommend this. JuMP’s workflow does not integrate well with Pluto. Even if the macro thing gets sorted (it may already be?), the mutation issue remains.

Previous discussions:

> [@JuMP and Pluto.jl?](https://discourse.julialang.org/t/jump-and-pluto-jl/49210):
>
> Hi, Has anyone tried using JuMP inside a Pluto.jl notebook? Are there any tricks one needs to be aware of? I have a simple notebook with two cells: “”" using JuMP, GLPK “”" and “”" begin M = Model(GLPK.Optimizer) @variable(M, test) end “”" The first time I run the second cell, it works, but subsequently, I get an error: “cannot assign a value to variable workspace767.test from module workspace768” (Note that the numbers are different, and that the 767 stays the same, whereas the …

> [@Cannot assign a value to variable workspace](https://discourse.julialang.org/t/cannot-assign-a-value-to-variable-workspace/56613/2):
>
> JuMP doesn’t work in pluto because pluto doesn’t understand macros or mutation of structs. Edit: here are the relevant issues: [https://github.com/fonsp/Pluto.jl/issues/196](https://github.com/fonsp/Pluto.jl/issues/196), [https://github.com/fonsp/Pluto.jl/issues/564](https://github.com/fonsp/Pluto.jl/issues/564)

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 18, 2021, 9:37pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/14 "2021-08-18T21:37:04Z")

</div>

Hi @odow,

Thank you very much for providing help so quickly. I tried some tricks, looked around in the discussions linked to this subject, and guessed that I would be in trouble. Anyway, I expected someone to come up with a trick that solves the problem as it happens in many other situations.

Unfortunately, it is too late to stop the migration process. Eleven out of 12 notebooks have been finished. This was the last one: optimization. If we cannot use a macro inside Pluto, that is quite bad. We use macros everywhere. I will have a go and try Neptune. This is for undergraduate teaching (economics, not computation), and it is a bad idea to mix up Pluto and VSCode or some other IDE. Too much for them.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [August 19, 2021, 6:33pm UTC](https://discourse.julialang.org/t/issue-with-modelingtoolkit-in-pluto/60517/15 "2021-08-19T18:33:53Z")

</div>

In the end, I managed to run the simple JuPM model described above inside Pluto.

With the help of @disberd, [here](https://discourse.julialang.org/t/announcing-neptune-jl-now-updated-to-multi-line-cells/54088/95), a solution like the one provided by VSCode, Atom, or Jupyter can be found in that link.
