IronPython.Runtime.UnboundNameException: name 'str' is not defined












3















I'm trying to run an IronPython script in C# but sometimes when I run it I get this error:



IronPython.Runtime.UnboundNameException: name 'str' is not defined


I can't figure out why this is happening; it only happens sometimes; right now it happens when I click a planet in my game to view the report on its abilities.



Here's the script I'm trying to run:



'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'


As you can see I'm trying to concatenate some strings, one of which is a stringified Amount1 variable (Amount1 I think is the number 800 but it might be a more complex expression equal to 800, I'd have to check on that), but for some reason I'm getting an error saying the str function is undefined, which makes no sense!



There are other scripts being included before this script runs; if you like I can find those and paste them here in case that might help...



edit: here's the full script that I'm running:



import sys, imp;
import __builtin__ as builtins;
builtins_code = """""";
exec builtins_code in builtins.__dict__;
import clr
clr.AddReference('System.Core')
import System
clr.ImportExtensions(System.Linq)
clr.AddReference('FrEee.Core')
import FrEee
import FrEee.Utility
clr.ImportExtensions(FrEee.Utility.Extensions)
from FrEee.Modding import Mod
from FrEee.Game.Objects.Space import Galaxy
from FrEee.Game.Objects.Civilization import Empire
'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'



Could the exec builtins_code line be deleting the str function? If so, how can I make sure that builtins_code gets added to builtins rather than replacing it?



edit2: nope, even if I remove the first four lines, it crashes when I process a turn in a single player game and then click my homeworld!



edit3: if I put the script in a text file and run it as a watch (see below) it runs just fine, even on the breakpoint where it crashes:



FrEee.Modding.ScriptEngine.EvaluateExpression<string>(System.IO.File.ReadAllText("c:/users/edkol/desktop/test.py")), ac










share|improve this question




















  • 1





    If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str?

    – BoarGules
    Jan 19 at 22:22











  • I don't see anywhere that I'm defining a variable called str...

    – ekolis
    Jan 20 at 14:55






  • 1





    The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect.

    – BoarGules
    Jan 20 at 15:22











  • Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins...

    – ekolis
    Jan 20 at 15:49











  • Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn!

    – ekolis
    Jan 21 at 3:16


















3















I'm trying to run an IronPython script in C# but sometimes when I run it I get this error:



IronPython.Runtime.UnboundNameException: name 'str' is not defined


I can't figure out why this is happening; it only happens sometimes; right now it happens when I click a planet in my game to view the report on its abilities.



Here's the script I'm trying to run:



'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'


As you can see I'm trying to concatenate some strings, one of which is a stringified Amount1 variable (Amount1 I think is the number 800 but it might be a more complex expression equal to 800, I'd have to check on that), but for some reason I'm getting an error saying the str function is undefined, which makes no sense!



There are other scripts being included before this script runs; if you like I can find those and paste them here in case that might help...



edit: here's the full script that I'm running:



import sys, imp;
import __builtin__ as builtins;
builtins_code = """""";
exec builtins_code in builtins.__dict__;
import clr
clr.AddReference('System.Core')
import System
clr.ImportExtensions(System.Linq)
clr.AddReference('FrEee.Core')
import FrEee
import FrEee.Utility
clr.ImportExtensions(FrEee.Utility.Extensions)
from FrEee.Modding import Mod
from FrEee.Game.Objects.Space import Galaxy
from FrEee.Game.Objects.Civilization import Empire
'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'



Could the exec builtins_code line be deleting the str function? If so, how can I make sure that builtins_code gets added to builtins rather than replacing it?



edit2: nope, even if I remove the first four lines, it crashes when I process a turn in a single player game and then click my homeworld!



edit3: if I put the script in a text file and run it as a watch (see below) it runs just fine, even on the breakpoint where it crashes:



FrEee.Modding.ScriptEngine.EvaluateExpression<string>(System.IO.File.ReadAllText("c:/users/edkol/desktop/test.py")), ac










share|improve this question




















  • 1





    If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str?

    – BoarGules
    Jan 19 at 22:22











  • I don't see anywhere that I'm defining a variable called str...

    – ekolis
    Jan 20 at 14:55






  • 1





    The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect.

    – BoarGules
    Jan 20 at 15:22











  • Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins...

    – ekolis
    Jan 20 at 15:49











  • Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn!

    – ekolis
    Jan 21 at 3:16
















3












3








3








I'm trying to run an IronPython script in C# but sometimes when I run it I get this error:



IronPython.Runtime.UnboundNameException: name 'str' is not defined


I can't figure out why this is happening; it only happens sometimes; right now it happens when I click a planet in my game to view the report on its abilities.



Here's the script I'm trying to run:



'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'


As you can see I'm trying to concatenate some strings, one of which is a stringified Amount1 variable (Amount1 I think is the number 800 but it might be a more complex expression equal to 800, I'd have to check on that), but for some reason I'm getting an error saying the str function is undefined, which makes no sense!



There are other scripts being included before this script runs; if you like I can find those and paste them here in case that might help...



edit: here's the full script that I'm running:



import sys, imp;
import __builtin__ as builtins;
builtins_code = """""";
exec builtins_code in builtins.__dict__;
import clr
clr.AddReference('System.Core')
import System
clr.ImportExtensions(System.Linq)
clr.AddReference('FrEee.Core')
import FrEee
import FrEee.Utility
clr.ImportExtensions(FrEee.Utility.Extensions)
from FrEee.Modding import Mod
from FrEee.Game.Objects.Space import Galaxy
from FrEee.Game.Objects.Civilization import Empire
'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'



Could the exec builtins_code line be deleting the str function? If so, how can I make sure that builtins_code gets added to builtins rather than replacing it?



edit2: nope, even if I remove the first four lines, it crashes when I process a turn in a single player game and then click my homeworld!



edit3: if I put the script in a text file and run it as a watch (see below) it runs just fine, even on the breakpoint where it crashes:



FrEee.Modding.ScriptEngine.EvaluateExpression<string>(System.IO.File.ReadAllText("c:/users/edkol/desktop/test.py")), ac










share|improve this question
















I'm trying to run an IronPython script in C# but sometimes when I run it I get this error:



IronPython.Runtime.UnboundNameException: name 'str' is not defined


I can't figure out why this is happening; it only happens sometimes; right now it happens when I click a planet in my game to view the report on its abilities.



Here's the script I'm trying to run:



'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'


As you can see I'm trying to concatenate some strings, one of which is a stringified Amount1 variable (Amount1 I think is the number 800 but it might be a more complex expression equal to 800, I'd have to check on that), but for some reason I'm getting an error saying the str function is undefined, which makes no sense!



There are other scripts being included before this script runs; if you like I can find those and paste them here in case that might help...



edit: here's the full script that I'm running:



import sys, imp;
import __builtin__ as builtins;
builtins_code = """""";
exec builtins_code in builtins.__dict__;
import clr
clr.AddReference('System.Core')
import System
clr.ImportExtensions(System.Linq)
clr.AddReference('FrEee.Core')
import FrEee
import FrEee.Utility
clr.ImportExtensions(FrEee.Utility.Extensions)
from FrEee.Modding import Mod
from FrEee.Game.Objects.Space import Galaxy
from FrEee.Game.Objects.Civilization import Empire
'Mines ' + str(Amount1) + ' minerals each turn (modified by planet value).'



Could the exec builtins_code line be deleting the str function? If so, how can I make sure that builtins_code gets added to builtins rather than replacing it?



edit2: nope, even if I remove the first four lines, it crashes when I process a turn in a single player game and then click my homeworld!



edit3: if I put the script in a text file and run it as a watch (see below) it runs just fine, even on the breakpoint where it crashes:



FrEee.Modding.ScriptEngine.EvaluateExpression<string>(System.IO.File.ReadAllText("c:/users/edkol/desktop/test.py")), ac







c# python ironpython






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 10:06







ekolis

















asked Jan 19 at 20:52









ekolisekolis

1,55721728




1,55721728








  • 1





    If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str?

    – BoarGules
    Jan 19 at 22:22











  • I don't see anywhere that I'm defining a variable called str...

    – ekolis
    Jan 20 at 14:55






  • 1





    The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect.

    – BoarGules
    Jan 20 at 15:22











  • Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins...

    – ekolis
    Jan 20 at 15:49











  • Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn!

    – ekolis
    Jan 21 at 3:16
















  • 1





    If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str?

    – BoarGules
    Jan 19 at 22:22











  • I don't see anywhere that I'm defining a variable called str...

    – ekolis
    Jan 20 at 14:55






  • 1





    The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect.

    – BoarGules
    Jan 20 at 15:22











  • Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins...

    – ekolis
    Jan 20 at 15:49











  • Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn!

    – ekolis
    Jan 21 at 3:16










1




1





If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str?

– BoarGules
Jan 19 at 22:22





If it only happens sometimes: is it possible that elsewhere in your code you have called a variable str?

– BoarGules
Jan 19 at 22:22













I don't see anywhere that I'm defining a variable called str...

– ekolis
Jan 20 at 14:55





I don't see anywhere that I'm defining a variable called str...

– ekolis
Jan 20 at 14:55




1




1





The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect.

– BoarGules
Jan 20 at 15:22





The only way I can reproduce your message in NameError: name 'str' is not defined in CPython 3 is to attempt del(str), which fails with that message. But IronPython isn't CPython so that may not mean much. Clearly something strange is going on in the mods to __builtins__, but that isn't telling you anything you didn't already suspect.

– BoarGules
Jan 20 at 15:22













Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins...

– ekolis
Jan 20 at 15:49





Yeah, if I remove the first four line which aren't really doing anything, then the script works. But that won't work if I have actual code to import into builtins...

– ekolis
Jan 20 at 15:49













Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn!

– ekolis
Jan 21 at 3:16







Hmm, still broken if I do the following steps: 1. start a new single player game, 2. process a turn, 3. open planet report - doesn't happen if I load a game without processing a turn!

– ekolis
Jan 21 at 3:16














1 Answer
1






active

oldest

votes


















3





+50









The line:



exec builtins_code in builtins.__dict__


just adds _ to the interactive environment so it repeats the last evaluated variable. Without that line, _ isn't defined. Note that this feature isn't very useful in a script and could be removed. This seems unrelated to your issue.



It looks like the sole interest of IronPython is to be able to interact easily with Microsoft/C# stuff, but it remains "stable" in 2.7 version and it's probably not as bug-free as the official python versions (similar bug was encountered and not solved on reddit, not that it helps, but you know you're not the only one...).



It has to be a bug because you cannot delete a builtin when it's not overridden:



>>> str
<type 'str'>
>>> del str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'str' is not defined


Now let's be pragmatic: there's no way someone is going to fix that bug in the near future.



So you could try to "restore" the str name by doing:



str = __builtins__.get('str')


I didn't test this, maybe it's not going to work either once you're in this strange state, but there are other workarounds to get hold of "lost" class names, using an instance and __class__ attribute:



>>> "".__class__(34)   # "".__class__ is str
'34'
>>> 0 .__class__(1.4) # note the space to help the parser :)
1


The only problem is that is going to look weird in your code. Don't forget to comment!



Anyway, you don't really need str for this, you can workaround your issue by using str.format on a format string (positional):



'Mines {} minerals each turn (modified by planet value).'.format(Amount1)


or by keyword:



'Mines {Amount1} minerals each turn (modified by planet value).'.format(Amount1=Amount1)


That method doesn't involve str built-in. Not saying that you're not going to have other problems, but even when str is available, this is one prefered way to format a string.



Work your way out by avoiding such errors. Fortunately, python exceptions are explicit and can be catched with a nice traceback.






share|improve this answer


























  • Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

    – ekolis
    Jan 29 at 23:52








  • 1





    no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

    – Jean-François Fabre
    Jan 30 at 8:01













  • Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

    – ekolis
    Jan 31 at 3:27











  • As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

    – ekolis
    Jan 31 at 3:32











  • check my last edit, you can do that all right. and thanks for the bounty, it's my first!

    – Jean-François Fabre
    Jan 31 at 9:15













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271292%2fironpython-runtime-unboundnameexception-name-str-is-not-defined%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3





+50









The line:



exec builtins_code in builtins.__dict__


just adds _ to the interactive environment so it repeats the last evaluated variable. Without that line, _ isn't defined. Note that this feature isn't very useful in a script and could be removed. This seems unrelated to your issue.



It looks like the sole interest of IronPython is to be able to interact easily with Microsoft/C# stuff, but it remains "stable" in 2.7 version and it's probably not as bug-free as the official python versions (similar bug was encountered and not solved on reddit, not that it helps, but you know you're not the only one...).



It has to be a bug because you cannot delete a builtin when it's not overridden:



>>> str
<type 'str'>
>>> del str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'str' is not defined


Now let's be pragmatic: there's no way someone is going to fix that bug in the near future.



So you could try to "restore" the str name by doing:



str = __builtins__.get('str')


I didn't test this, maybe it's not going to work either once you're in this strange state, but there are other workarounds to get hold of "lost" class names, using an instance and __class__ attribute:



>>> "".__class__(34)   # "".__class__ is str
'34'
>>> 0 .__class__(1.4) # note the space to help the parser :)
1


The only problem is that is going to look weird in your code. Don't forget to comment!



Anyway, you don't really need str for this, you can workaround your issue by using str.format on a format string (positional):



'Mines {} minerals each turn (modified by planet value).'.format(Amount1)


or by keyword:



'Mines {Amount1} minerals each turn (modified by planet value).'.format(Amount1=Amount1)


That method doesn't involve str built-in. Not saying that you're not going to have other problems, but even when str is available, this is one prefered way to format a string.



Work your way out by avoiding such errors. Fortunately, python exceptions are explicit and can be catched with a nice traceback.






share|improve this answer


























  • Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

    – ekolis
    Jan 29 at 23:52








  • 1





    no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

    – Jean-François Fabre
    Jan 30 at 8:01













  • Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

    – ekolis
    Jan 31 at 3:27











  • As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

    – ekolis
    Jan 31 at 3:32











  • check my last edit, you can do that all right. and thanks for the bounty, it's my first!

    – Jean-François Fabre
    Jan 31 at 9:15


















3





+50









The line:



exec builtins_code in builtins.__dict__


just adds _ to the interactive environment so it repeats the last evaluated variable. Without that line, _ isn't defined. Note that this feature isn't very useful in a script and could be removed. This seems unrelated to your issue.



It looks like the sole interest of IronPython is to be able to interact easily with Microsoft/C# stuff, but it remains "stable" in 2.7 version and it's probably not as bug-free as the official python versions (similar bug was encountered and not solved on reddit, not that it helps, but you know you're not the only one...).



It has to be a bug because you cannot delete a builtin when it's not overridden:



>>> str
<type 'str'>
>>> del str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'str' is not defined


Now let's be pragmatic: there's no way someone is going to fix that bug in the near future.



So you could try to "restore" the str name by doing:



str = __builtins__.get('str')


I didn't test this, maybe it's not going to work either once you're in this strange state, but there are other workarounds to get hold of "lost" class names, using an instance and __class__ attribute:



>>> "".__class__(34)   # "".__class__ is str
'34'
>>> 0 .__class__(1.4) # note the space to help the parser :)
1


The only problem is that is going to look weird in your code. Don't forget to comment!



Anyway, you don't really need str for this, you can workaround your issue by using str.format on a format string (positional):



'Mines {} minerals each turn (modified by planet value).'.format(Amount1)


or by keyword:



'Mines {Amount1} minerals each turn (modified by planet value).'.format(Amount1=Amount1)


That method doesn't involve str built-in. Not saying that you're not going to have other problems, but even when str is available, this is one prefered way to format a string.



Work your way out by avoiding such errors. Fortunately, python exceptions are explicit and can be catched with a nice traceback.






share|improve this answer


























  • Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

    – ekolis
    Jan 29 at 23:52








  • 1





    no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

    – Jean-François Fabre
    Jan 30 at 8:01













  • Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

    – ekolis
    Jan 31 at 3:27











  • As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

    – ekolis
    Jan 31 at 3:32











  • check my last edit, you can do that all right. and thanks for the bounty, it's my first!

    – Jean-François Fabre
    Jan 31 at 9:15
















3





+50







3





+50



3




+50





The line:



exec builtins_code in builtins.__dict__


just adds _ to the interactive environment so it repeats the last evaluated variable. Without that line, _ isn't defined. Note that this feature isn't very useful in a script and could be removed. This seems unrelated to your issue.



It looks like the sole interest of IronPython is to be able to interact easily with Microsoft/C# stuff, but it remains "stable" in 2.7 version and it's probably not as bug-free as the official python versions (similar bug was encountered and not solved on reddit, not that it helps, but you know you're not the only one...).



It has to be a bug because you cannot delete a builtin when it's not overridden:



>>> str
<type 'str'>
>>> del str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'str' is not defined


Now let's be pragmatic: there's no way someone is going to fix that bug in the near future.



So you could try to "restore" the str name by doing:



str = __builtins__.get('str')


I didn't test this, maybe it's not going to work either once you're in this strange state, but there are other workarounds to get hold of "lost" class names, using an instance and __class__ attribute:



>>> "".__class__(34)   # "".__class__ is str
'34'
>>> 0 .__class__(1.4) # note the space to help the parser :)
1


The only problem is that is going to look weird in your code. Don't forget to comment!



Anyway, you don't really need str for this, you can workaround your issue by using str.format on a format string (positional):



'Mines {} minerals each turn (modified by planet value).'.format(Amount1)


or by keyword:



'Mines {Amount1} minerals each turn (modified by planet value).'.format(Amount1=Amount1)


That method doesn't involve str built-in. Not saying that you're not going to have other problems, but even when str is available, this is one prefered way to format a string.



Work your way out by avoiding such errors. Fortunately, python exceptions are explicit and can be catched with a nice traceback.






share|improve this answer















The line:



exec builtins_code in builtins.__dict__


just adds _ to the interactive environment so it repeats the last evaluated variable. Without that line, _ isn't defined. Note that this feature isn't very useful in a script and could be removed. This seems unrelated to your issue.



It looks like the sole interest of IronPython is to be able to interact easily with Microsoft/C# stuff, but it remains "stable" in 2.7 version and it's probably not as bug-free as the official python versions (similar bug was encountered and not solved on reddit, not that it helps, but you know you're not the only one...).



It has to be a bug because you cannot delete a builtin when it's not overridden:



>>> str
<type 'str'>
>>> del str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'str' is not defined


Now let's be pragmatic: there's no way someone is going to fix that bug in the near future.



So you could try to "restore" the str name by doing:



str = __builtins__.get('str')


I didn't test this, maybe it's not going to work either once you're in this strange state, but there are other workarounds to get hold of "lost" class names, using an instance and __class__ attribute:



>>> "".__class__(34)   # "".__class__ is str
'34'
>>> 0 .__class__(1.4) # note the space to help the parser :)
1


The only problem is that is going to look weird in your code. Don't forget to comment!



Anyway, you don't really need str for this, you can workaround your issue by using str.format on a format string (positional):



'Mines {} minerals each turn (modified by planet value).'.format(Amount1)


or by keyword:



'Mines {Amount1} minerals each turn (modified by planet value).'.format(Amount1=Amount1)


That method doesn't involve str built-in. Not saying that you're not going to have other problems, but even when str is available, this is one prefered way to format a string.



Work your way out by avoiding such errors. Fortunately, python exceptions are explicit and can be catched with a nice traceback.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 31 at 9:35

























answered Jan 29 at 20:16









Jean-François FabreJean-François Fabre

103k955112




103k955112













  • Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

    – ekolis
    Jan 29 at 23:52








  • 1





    no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

    – Jean-François Fabre
    Jan 30 at 8:01













  • Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

    – ekolis
    Jan 31 at 3:27











  • As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

    – ekolis
    Jan 31 at 3:32











  • check my last edit, you can do that all right. and thanks for the bounty, it's my first!

    – Jean-François Fabre
    Jan 31 at 9:15





















  • Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

    – ekolis
    Jan 29 at 23:52








  • 1





    no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

    – Jean-François Fabre
    Jan 30 at 8:01













  • Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

    – ekolis
    Jan 31 at 3:27











  • As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

    – ekolis
    Jan 31 at 3:32











  • check my last edit, you can do that all right. and thanks for the bounty, it's my first!

    – Jean-François Fabre
    Jan 31 at 9:15



















Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

– ekolis
Jan 29 at 23:52







Wait is it __builtin__ or __builtins__? Maybe that's screwing me up somehow, if I spelled it wrong...

– ekolis
Jan 29 at 23:52






1




1





no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

– Jean-François Fabre
Jan 30 at 8:01







no, both are related on python 2.7. See stackoverflow.com/questions/11181519/…. One is the dict of the other. But those lines are useless when not interactive.

– Jean-François Fabre
Jan 30 at 8:01















Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

– ekolis
Jan 31 at 3:27





Hmm, I tried getting str from the __builtins__ and I got an error saying __builtins__ was not defined! Tried it also with __builtin__ and got a similar error...

– ekolis
Jan 31 at 3:27













As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

– ekolis
Jan 31 at 3:32





As for the format call, that's going to be tricky because I'm generating the script from a string interpolation sort of thing, e.g. "Mines {Amount1} minerals per turn"...

– ekolis
Jan 31 at 3:32













check my last edit, you can do that all right. and thanks for the bounty, it's my first!

– Jean-François Fabre
Jan 31 at 9:15







check my last edit, you can do that all right. and thanks for the bounty, it's my first!

– Jean-François Fabre
Jan 31 at 9:15




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54271292%2fironpython-runtime-unboundnameexception-name-str-is-not-defined%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage