# Python exec() command

**URL:** https://forum.dynamobim.com/t/python-exec-command/23769
**Category:** Uncategorized
**Created:** [July 16, 2018, 7:03am UTC](https://forum.dynamobim.com/t/python-exec-command/23769 "2018-07-16T07:03:18Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)
#### Post date: [July 16, 2018, 7:03am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/1 "2018-07-16T07:03:18Z")

</div>

following problem:  
i have a string i want to execute like:

string = “2\*A+B”  
usually it should work like result = exec string.  
when i try that in dynamo i receive only error messages.

any clue?

---

<div class="post-metadata">

### Author: ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)
#### Post date: [July 16, 2018, 7:06am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/2 "2018-07-16T07:06:26Z")

</div>

Please show a screenshot…

Generally you cannot perform math on strings… (Neither in Python nor in Dynamo)

---

<div class="post-metadata">

### Author: ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)
#### Post date: [July 16, 2018, 7:18am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/3 "2018-07-16T07:18:48Z")

</div>

Posting the error and the code would definitely help. Exec should work fine inside of Dynamo:

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/c/dc7f223f820bc55252b6489e67238ad622d303f9.png)

You’re possibly trying to reference an argument before assigning it. Maybe having keyword arguments would be easier in that case?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/c/0c96390290619f745b5d0e512fa7f004e728e3b6.png)

---

<div class="post-metadata">

### Author: ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)
#### Post date: [July 16, 2018, 7:22am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/4 "2018-07-16T07:22:54Z")

</div>

But using strings will give wrong results, at least that’s was what I meant in my original reply 😉  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/6/464495598046894418d2929c5b83f25c13ea1855.png)

Alternatively you can import the “math” module in Python  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/6/263e2a68cfc5503f6467ce3eefcfdd04460a76b3.png)

---

<div class="post-metadata">

### Author: ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)
#### Post date: [July 16, 2018, 7:28am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/5 "2018-07-16T07:28:24Z")

</div>

dimitar thank you for your reply.  
just found out that it is a problem with dynamo 2+  
going back to 1.3 everything is working.  
btw code is just the same as your smple 😉  
tx peter

---

<div class="post-metadata">

### Author: ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)
#### Post date: [July 16, 2018, 7:31am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/6 "2018-07-16T07:31:55Z")

</div>

dimitar, asfaik math is not capable of calculating strings… true?

---

<div class="post-metadata">

### Author: ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)
#### Post date: [July 16, 2018, 7:54am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/7 "2018-07-16T07:54:46Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/3/8331adaa4695f64087c991165265ef3538186963.png)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/b/6bcfcb3467ce10530efc1b9fc052b245dc04c566.png)

weird thing it that dynamo doe not calculate but writes output multiple times

---

<div class="post-metadata">

### Author: ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)
#### Post date: [July 16, 2018, 8:11am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/8 "2018-07-16T08:11:34Z")

</div>

It seems that it is repeated the number of times that your formula states?  
E.g. “NAF\*2” is repeated twice…

Can you show your entire script and possibly the input from Dynamo if you have that? It would make it easier to understand… 🙂

---

<div class="post-metadata">

### Author: ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)
#### Post date: [July 16, 2018, 9:27am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/9 "2018-07-16T09:27:16Z")

</div>

Exactly that is the Point ?!

Architekturbüro DI Peter Kompolschek  
Pestalozzistr. 11/60  
AT 9500 Villach  
+43 664 46 17 873  
www.kompolschek.at

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/7/677f06bdf711468707faf594c72c7993f90d2513.png)

---

<div class="post-metadata">

### Author: ![Dimitar\_Venkov](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/dimitar_venkov/32/2068_2.png) [@Dimitar\_Venkov](https://forum.dynamobim.com/u/Dimitar_Venkov)
#### Post date: [July 16, 2018, 9:35am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/10 "2018-07-16T09:35:09Z")

</div>

If any of your input arguments are strings and you want to use math on them, you’ll first have to convert them to a numeric object. For example:

> A = float(“1.23”)  
> B = float(“4.56”)

---

<div class="post-metadata">

### Author: ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)
#### Post date: [July 16, 2018, 9:54am UTC](https://forum.dynamobim.com/t/python-exec-command/23769/11 "2018-07-16T09:54:13Z")

</div>

dimitar thank you.  
thought that even variables shall be string.  
by converting them to numbers solved the problem
