python importing a function in which I define a global variable [duplicate]












-2
















This question already has an answer here:




  • Get variable from another file - python

    1 answer




I'm having difficulty accessing a variable.



I'm working toward the following, calling python script from bash, with arguments, which then imports a function defined in a second python script, executes it and thereby creates a variable which will be used later in the first python script.



At the moment,to test I'm copying and pasting directly into a Python terminal some commands like this:



from script2 import *
foofunction(arg)
print(newlist)


The foo function defined in script2 is executing, I can see files have been written but when I try to print the list supposedly created while executing the imported function, I get a message telling me it's undefined.



Inside my script2.py, i made sure to enter a statement
global newlist
before defining its length and populating it.



I'm scratching my head at this stage.










share|improve this question















marked as duplicate by TheIncorrigible1, Idlehands, Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 18 at 16:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3





    Please include the relevant code from script2.py :)

    – KuboMD
    Jan 18 at 16:08






  • 2





    Why don't you return newlist in that function and assign it to a variable?

    – Graipher
    Jan 18 at 16:08






  • 1





    The global keyword does not work that way in python. Have a look here

    – Valentino
    Jan 18 at 16:11











  • I don't know why that didn't work when I tried it last night, but Graipher's solution is exactly correct. IF you want to throw that into an answer I'll gladly accept it.

    – curly
    Jan 18 at 16:18






  • 2





    from some_module import * is rarely a good practice as it mangles up your namespace. This is a prime example - without the knowledge of script2.py we have no idea whether foofunction and newlist would be a NameError or an object that was defined in script2.py. It's best to be explicit.

    – Idlehands
    Jan 18 at 16:19
















-2
















This question already has an answer here:




  • Get variable from another file - python

    1 answer




I'm having difficulty accessing a variable.



I'm working toward the following, calling python script from bash, with arguments, which then imports a function defined in a second python script, executes it and thereby creates a variable which will be used later in the first python script.



At the moment,to test I'm copying and pasting directly into a Python terminal some commands like this:



from script2 import *
foofunction(arg)
print(newlist)


The foo function defined in script2 is executing, I can see files have been written but when I try to print the list supposedly created while executing the imported function, I get a message telling me it's undefined.



Inside my script2.py, i made sure to enter a statement
global newlist
before defining its length and populating it.



I'm scratching my head at this stage.










share|improve this question















marked as duplicate by TheIncorrigible1, Idlehands, Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 18 at 16:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3





    Please include the relevant code from script2.py :)

    – KuboMD
    Jan 18 at 16:08






  • 2





    Why don't you return newlist in that function and assign it to a variable?

    – Graipher
    Jan 18 at 16:08






  • 1





    The global keyword does not work that way in python. Have a look here

    – Valentino
    Jan 18 at 16:11











  • I don't know why that didn't work when I tried it last night, but Graipher's solution is exactly correct. IF you want to throw that into an answer I'll gladly accept it.

    – curly
    Jan 18 at 16:18






  • 2





    from some_module import * is rarely a good practice as it mangles up your namespace. This is a prime example - without the knowledge of script2.py we have no idea whether foofunction and newlist would be a NameError or an object that was defined in script2.py. It's best to be explicit.

    – Idlehands
    Jan 18 at 16:19














-2












-2








-2









This question already has an answer here:




  • Get variable from another file - python

    1 answer




I'm having difficulty accessing a variable.



I'm working toward the following, calling python script from bash, with arguments, which then imports a function defined in a second python script, executes it and thereby creates a variable which will be used later in the first python script.



At the moment,to test I'm copying and pasting directly into a Python terminal some commands like this:



from script2 import *
foofunction(arg)
print(newlist)


The foo function defined in script2 is executing, I can see files have been written but when I try to print the list supposedly created while executing the imported function, I get a message telling me it's undefined.



Inside my script2.py, i made sure to enter a statement
global newlist
before defining its length and populating it.



I'm scratching my head at this stage.










share|improve this question

















This question already has an answer here:




  • Get variable from another file - python

    1 answer




I'm having difficulty accessing a variable.



I'm working toward the following, calling python script from bash, with arguments, which then imports a function defined in a second python script, executes it and thereby creates a variable which will be used later in the first python script.



At the moment,to test I'm copying and pasting directly into a Python terminal some commands like this:



from script2 import *
foofunction(arg)
print(newlist)


The foo function defined in script2 is executing, I can see files have been written but when I try to print the list supposedly created while executing the imported function, I get a message telling me it's undefined.



Inside my script2.py, i made sure to enter a statement
global newlist
before defining its length and populating it.



I'm scratching my head at this stage.





This question already has an answer here:




  • Get variable from another file - python

    1 answer








python variables global






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 17:49









Tomka Koliada

497117




497117










asked Jan 18 at 16:07









curlycurly

135




135




marked as duplicate by TheIncorrigible1, Idlehands, Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 18 at 16:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by TheIncorrigible1, Idlehands, Prune python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 18 at 16:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    Please include the relevant code from script2.py :)

    – KuboMD
    Jan 18 at 16:08






  • 2





    Why don't you return newlist in that function and assign it to a variable?

    – Graipher
    Jan 18 at 16:08






  • 1





    The global keyword does not work that way in python. Have a look here

    – Valentino
    Jan 18 at 16:11











  • I don't know why that didn't work when I tried it last night, but Graipher's solution is exactly correct. IF you want to throw that into an answer I'll gladly accept it.

    – curly
    Jan 18 at 16:18






  • 2





    from some_module import * is rarely a good practice as it mangles up your namespace. This is a prime example - without the knowledge of script2.py we have no idea whether foofunction and newlist would be a NameError or an object that was defined in script2.py. It's best to be explicit.

    – Idlehands
    Jan 18 at 16:19














  • 3





    Please include the relevant code from script2.py :)

    – KuboMD
    Jan 18 at 16:08






  • 2





    Why don't you return newlist in that function and assign it to a variable?

    – Graipher
    Jan 18 at 16:08






  • 1





    The global keyword does not work that way in python. Have a look here

    – Valentino
    Jan 18 at 16:11











  • I don't know why that didn't work when I tried it last night, but Graipher's solution is exactly correct. IF you want to throw that into an answer I'll gladly accept it.

    – curly
    Jan 18 at 16:18






  • 2





    from some_module import * is rarely a good practice as it mangles up your namespace. This is a prime example - without the knowledge of script2.py we have no idea whether foofunction and newlist would be a NameError or an object that was defined in script2.py. It's best to be explicit.

    – Idlehands
    Jan 18 at 16:19








3




3





Please include the relevant code from script2.py :)

– KuboMD
Jan 18 at 16:08





Please include the relevant code from script2.py :)

– KuboMD
Jan 18 at 16:08




2




2





Why don't you return newlist in that function and assign it to a variable?

– Graipher
Jan 18 at 16:08





Why don't you return newlist in that function and assign it to a variable?

– Graipher
Jan 18 at 16:08




1




1





The global keyword does not work that way in python. Have a look here

– Valentino
Jan 18 at 16:11





The global keyword does not work that way in python. Have a look here

– Valentino
Jan 18 at 16:11













I don't know why that didn't work when I tried it last night, but Graipher's solution is exactly correct. IF you want to throw that into an answer I'll gladly accept it.

– curly
Jan 18 at 16:18





I don't know why that didn't work when I tried it last night, but Graipher's solution is exactly correct. IF you want to throw that into an answer I'll gladly accept it.

– curly
Jan 18 at 16:18




2




2





from some_module import * is rarely a good practice as it mangles up your namespace. This is a prime example - without the knowledge of script2.py we have no idea whether foofunction and newlist would be a NameError or an object that was defined in script2.py. It's best to be explicit.

– Idlehands
Jan 18 at 16:19





from some_module import * is rarely a good practice as it mangles up your namespace. This is a prime example - without the knowledge of script2.py we have no idea whether foofunction and newlist would be a NameError or an object that was defined in script2.py. It's best to be explicit.

– Idlehands
Jan 18 at 16:19












1 Answer
1






active

oldest

votes


















-2














You should refer to newlist and the module where it is defined like this:



from script2 import *
foofunction(arg)
print(script2.newlist)





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -2














    You should refer to newlist and the module where it is defined like this:



    from script2 import *
    foofunction(arg)
    print(script2.newlist)





    share|improve this answer




























      -2














      You should refer to newlist and the module where it is defined like this:



      from script2 import *
      foofunction(arg)
      print(script2.newlist)





      share|improve this answer


























        -2












        -2








        -2







        You should refer to newlist and the module where it is defined like this:



        from script2 import *
        foofunction(arg)
        print(script2.newlist)





        share|improve this answer













        You should refer to newlist and the module where it is defined like this:



        from script2 import *
        foofunction(arg)
        print(script2.newlist)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 16:28









        nachonacho

        2,65411223




        2,65411223















            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre