# Code execution problem

**URL:** <https://discourse.julialang.org/t/code-execution-problem/31307>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 20, 2019, 10:01am UTC](https://discourse.julialang.org/t/code-execution-problem/31307 "2019-11-20T10:01:59Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 20, 2019, 10:01am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/1 "2019-11-20T10:01:59Z")

</div>

![%E6%89%B9%E6%B3%A8%202019-11-20%20180025](https://global.discourse-cdn.com/julialang/original/3X/1/1/119ca7833e1479b9a7debddf92fa543801c73e8b.png)

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 20, 2019, 10:15am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/2 "2019-11-20T10:15:32Z")

</div>

Hi @dty, welcome to Julia!

Please provide as much context as possible to your question, formulate it as a minimal working example, and do not use screen-shots but actual text (so people can copy paste to try it out). Remember, when answering questions, people are gifting their time to you; so you should make it as easy as possible for them to help you!

A good read is also [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 20, 2019, 10:38am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/3 "2019-11-20T10:38:49Z")

</div>

`@variable` is from JuMP, no? So you probably need `using JuMP`.

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 21, 2019, 1:56am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/4 "2019-11-21T01:56:44Z")

</div>

yes.I have installed jump,but @variable is also error.

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 21, 2019, 1:57am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/5 "2019-11-21T01:57:28Z")

</div>

OK,thanks

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 21, 2019, 4:31am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/6 "2019-11-21T04:31:57Z")

</div>

As @kristoffer.carlsson said , before defining your function you need to write explicitly:

```julia
using JuMP

function blablabla()

end

```

it is not enought to have the package installed, you have to execute `using JuMP` to actually have access to its functions.

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 21, 2019, 11:31am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/7 "2019-11-21T11:31:18Z")

</div>

i have try it.but it still error

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 21, 2019, 11:32am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/8 "2019-11-21T11:32:21Z")

</div>

![1](https://global.discourse-cdn.com/julialang/original/3X/f/9/f990fbda20f075e7cae847a7f5579c7445914eb5.png)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 21, 2019, 12:34pm UTC](https://discourse.julialang.org/t/code-execution-problem/31307/9 "2019-11-21T12:34:53Z")

</div>

Execute only the `using JuMP` line first. Then the rest.

---

<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:** [November 21, 2019, 12:46pm UTC](https://discourse.julialang.org/t/code-execution-problem/31307/10 "2019-11-21T12:46:10Z")

</div>

Your example is also using an old version of JuMP.

Read the documentation [Introduction · JuMP](http://www.juliaopt.org/JuMP.jl/v0.20.0/)  
For the new syntax. The quick start guide is a good starting point.

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 21, 2019, 1:56pm UTC](https://discourse.julialang.org/t/code-execution-problem/31307/11 "2019-11-21T13:56:18Z")

</div>

it still same error:pensive:

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 21, 2019, 4:47pm UTC](https://discourse.julialang.org/t/code-execution-problem/31307/12 "2019-11-21T16:47:29Z")

</div>

Hi dty:

You have received some suggestions, but it is difficult to help if you just show us screenshots, remember to read the advice given to you on the first answer.

Try to please update your packages using `]up` in the REPL.  
Check the documentation given by @odow and adapt your code.

If it still doesn’t work, please copy here your code (not a screenshot) so that somebody else can try and run it.

Also, please tell us with detail how are you running it (are you sending the whole block to the REPL?, does `using JuMP` work without any error message?)

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 22, 2019, 1:31am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/14 "2019-11-22T01:31:14Z")

</div>

this is my code:

```julia
using Pkg
Pkg.add("MathOptInterface")
using Pkg
Pkg.add("JuMP")
using Pkg
Pkg.add("Interact")
using Pkg
Pkg.add("Gadfly")
const g_max = [1000,1000]
const g_min = [0,300]
const c_g = [50,100]
const c_g0 = [1000,0]
const c_w = 50
const d = 1500
const w_f = 200
function opt_ed(g_max,g_min,c_g,c_w,d,w_f)
ed = Model()
@defVar(ed,0 < g[i=1:2] <= g_max[i])
@defVar(ed,0 <= w <= w_f)
@setObjective(ed,Min,sum(c_g[i]*g[i],i = 1:2) + c_w*w)
for i in 1:2
    @addConstraint(ed,g[i] <= g_max[i])
    @addConstraint(ed,g[i] >= g_min[i])
@addConstraint(ed,w <= w_f)
@addConstraint(ed,sum(g([i],i = 1:2) + w == d))
end
solve(ed)
(g_opt,w_opt,ws_opt,obj) = solved(g_max,g_min,c_g,c_w,d,w_f);
println("\n")
println("Dispatch of Generators: ", g_opt[i=1:2], " MW")
println("Dispatch of Wind: ", w_opt, " MW")
println("Wind spillage: ", w_f-w_opt, " MW")
println("\n")
println("Total cost: ", obj, "\$")
end

```

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 22, 2019, 1:54am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/15 "2019-11-22T01:54:26Z")

</div>

i’m using the IDE is Juno

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 22, 2019, 9:12am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/16 "2019-11-22T09:12:09Z")

</div>

You still don’t have a `using JuMP` in your code – `Pkg.add`ing a package isn’t enough to use it.

---

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [November 23, 2019, 8:19am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/17 "2019-11-23T08:19:50Z")

</div>

so,firstly, add (“JuMP”) in Pkg model,in addition to this,what should i do?

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [November 23, 2019, 11:06am UTC](https://discourse.julialang.org/t/code-execution-problem/31307/18 "2019-11-23T11:06:36Z")

</div>

You do not have to add packages several times, you could replace

```julia
using Pkg
Pkg.add("MathOptInterface")
using Pkg
Pkg.add("JuMP")
using Pkg
Pkg.add("Interact")
using Pkg
Pkg.add("Gadfly")

```

with

```julia
using Pkg
Pkg.add("MathOptInterface")
Pkg.add("JuMP")
Pkg.add("Interact")
Pkg.add("Gadfly")

```

and run it only once. Then, it should be installed in your computer, and you do not need it more. In your code, you must add at the beginning:

```julia
using JuMP
using MathOptInterface
using Interact
using Gadfly

```

Please, read the documentation about modules. To use a module in your code you must use

```julia
using PackageName

```

and then, you can use the functions, macros defined in the package.

I suggest you to see learning materials in [Get started with Julia](https://julialang.org/learning/) or in [https://juliaacademy.com/](https://juliaacademy.com/) the free Julia Introduction course (with videos and examples), that includes this type of contents.
