Access numpy array (vs. python array) from another module












-1















I am tryint to convert some of my python arrays to numpy arrays and have problems accessing a supposingly global np array in another module.



Module 1 (imports data):



import numpy as np
jobs_db =

def read_all_data(date, filepath):

global jobs_db
jobs_db = np.loadtxt(filepath+'jobs_input.csv', dtype=np.uint8, delimiter=",", skiprows=1)


Module 2 (uses data):



from Import_data import *

if __name__ == '__main__':

read_all_data(180901, 'C:/Users/*********/')
print(jobs_db)


However, when I execute the main method, the console shows an empty array while the array contains data when calling it whithin module 1. The problem does not occur if I use a python array instead of a numpy array.










share|improve this question


















  • 2





    Why are you using global variables here at all? Just return it from your function

    – juanpa.arrivillaga
    Nov 21 '18 at 20:26











  • Anyway, this is because you are using a starred import, which is essentially equivalent to import Import_data; jobs_db = Import_data.jobs_db, but now jobs_db has nothing to do with Import_data.jobs_db, except they happen to refer to the same object, but can be made to refer to other objects independently. Use the module, so Import_data.jobs_db, or better yet, don't use a global variable at all

    – juanpa.arrivillaga
    Nov 21 '18 at 20:28











  • But how come it works with a python array and not a numpy array?

    – Hendrik
    Nov 21 '18 at 21:06











  • What do you mean? It works in exactly the same way.

    – juanpa.arrivillaga
    Nov 21 '18 at 21:07











  • Try it for yourself, make your function just do jobs_db = ['foo', 'bar']

    – juanpa.arrivillaga
    Nov 21 '18 at 21:14
















-1















I am tryint to convert some of my python arrays to numpy arrays and have problems accessing a supposingly global np array in another module.



Module 1 (imports data):



import numpy as np
jobs_db =

def read_all_data(date, filepath):

global jobs_db
jobs_db = np.loadtxt(filepath+'jobs_input.csv', dtype=np.uint8, delimiter=",", skiprows=1)


Module 2 (uses data):



from Import_data import *

if __name__ == '__main__':

read_all_data(180901, 'C:/Users/*********/')
print(jobs_db)


However, when I execute the main method, the console shows an empty array while the array contains data when calling it whithin module 1. The problem does not occur if I use a python array instead of a numpy array.










share|improve this question


















  • 2





    Why are you using global variables here at all? Just return it from your function

    – juanpa.arrivillaga
    Nov 21 '18 at 20:26











  • Anyway, this is because you are using a starred import, which is essentially equivalent to import Import_data; jobs_db = Import_data.jobs_db, but now jobs_db has nothing to do with Import_data.jobs_db, except they happen to refer to the same object, but can be made to refer to other objects independently. Use the module, so Import_data.jobs_db, or better yet, don't use a global variable at all

    – juanpa.arrivillaga
    Nov 21 '18 at 20:28











  • But how come it works with a python array and not a numpy array?

    – Hendrik
    Nov 21 '18 at 21:06











  • What do you mean? It works in exactly the same way.

    – juanpa.arrivillaga
    Nov 21 '18 at 21:07











  • Try it for yourself, make your function just do jobs_db = ['foo', 'bar']

    – juanpa.arrivillaga
    Nov 21 '18 at 21:14














-1












-1








-1








I am tryint to convert some of my python arrays to numpy arrays and have problems accessing a supposingly global np array in another module.



Module 1 (imports data):



import numpy as np
jobs_db =

def read_all_data(date, filepath):

global jobs_db
jobs_db = np.loadtxt(filepath+'jobs_input.csv', dtype=np.uint8, delimiter=",", skiprows=1)


Module 2 (uses data):



from Import_data import *

if __name__ == '__main__':

read_all_data(180901, 'C:/Users/*********/')
print(jobs_db)


However, when I execute the main method, the console shows an empty array while the array contains data when calling it whithin module 1. The problem does not occur if I use a python array instead of a numpy array.










share|improve this question














I am tryint to convert some of my python arrays to numpy arrays and have problems accessing a supposingly global np array in another module.



Module 1 (imports data):



import numpy as np
jobs_db =

def read_all_data(date, filepath):

global jobs_db
jobs_db = np.loadtxt(filepath+'jobs_input.csv', dtype=np.uint8, delimiter=",", skiprows=1)


Module 2 (uses data):



from Import_data import *

if __name__ == '__main__':

read_all_data(180901, 'C:/Users/*********/')
print(jobs_db)


However, when I execute the main method, the console shows an empty array while the array contains data when calling it whithin module 1. The problem does not occur if I use a python array instead of a numpy array.







python arrays numpy global-variables local-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 20:18









HendrikHendrik

6513




6513








  • 2





    Why are you using global variables here at all? Just return it from your function

    – juanpa.arrivillaga
    Nov 21 '18 at 20:26











  • Anyway, this is because you are using a starred import, which is essentially equivalent to import Import_data; jobs_db = Import_data.jobs_db, but now jobs_db has nothing to do with Import_data.jobs_db, except they happen to refer to the same object, but can be made to refer to other objects independently. Use the module, so Import_data.jobs_db, or better yet, don't use a global variable at all

    – juanpa.arrivillaga
    Nov 21 '18 at 20:28











  • But how come it works with a python array and not a numpy array?

    – Hendrik
    Nov 21 '18 at 21:06











  • What do you mean? It works in exactly the same way.

    – juanpa.arrivillaga
    Nov 21 '18 at 21:07











  • Try it for yourself, make your function just do jobs_db = ['foo', 'bar']

    – juanpa.arrivillaga
    Nov 21 '18 at 21:14














  • 2





    Why are you using global variables here at all? Just return it from your function

    – juanpa.arrivillaga
    Nov 21 '18 at 20:26











  • Anyway, this is because you are using a starred import, which is essentially equivalent to import Import_data; jobs_db = Import_data.jobs_db, but now jobs_db has nothing to do with Import_data.jobs_db, except they happen to refer to the same object, but can be made to refer to other objects independently. Use the module, so Import_data.jobs_db, or better yet, don't use a global variable at all

    – juanpa.arrivillaga
    Nov 21 '18 at 20:28











  • But how come it works with a python array and not a numpy array?

    – Hendrik
    Nov 21 '18 at 21:06











  • What do you mean? It works in exactly the same way.

    – juanpa.arrivillaga
    Nov 21 '18 at 21:07











  • Try it for yourself, make your function just do jobs_db = ['foo', 'bar']

    – juanpa.arrivillaga
    Nov 21 '18 at 21:14








2




2





Why are you using global variables here at all? Just return it from your function

– juanpa.arrivillaga
Nov 21 '18 at 20:26





Why are you using global variables here at all? Just return it from your function

– juanpa.arrivillaga
Nov 21 '18 at 20:26













Anyway, this is because you are using a starred import, which is essentially equivalent to import Import_data; jobs_db = Import_data.jobs_db, but now jobs_db has nothing to do with Import_data.jobs_db, except they happen to refer to the same object, but can be made to refer to other objects independently. Use the module, so Import_data.jobs_db, or better yet, don't use a global variable at all

– juanpa.arrivillaga
Nov 21 '18 at 20:28





Anyway, this is because you are using a starred import, which is essentially equivalent to import Import_data; jobs_db = Import_data.jobs_db, but now jobs_db has nothing to do with Import_data.jobs_db, except they happen to refer to the same object, but can be made to refer to other objects independently. Use the module, so Import_data.jobs_db, or better yet, don't use a global variable at all

– juanpa.arrivillaga
Nov 21 '18 at 20:28













But how come it works with a python array and not a numpy array?

– Hendrik
Nov 21 '18 at 21:06





But how come it works with a python array and not a numpy array?

– Hendrik
Nov 21 '18 at 21:06













What do you mean? It works in exactly the same way.

– juanpa.arrivillaga
Nov 21 '18 at 21:07





What do you mean? It works in exactly the same way.

– juanpa.arrivillaga
Nov 21 '18 at 21:07













Try it for yourself, make your function just do jobs_db = ['foo', 'bar']

– juanpa.arrivillaga
Nov 21 '18 at 21:14





Try it for yourself, make your function just do jobs_db = ['foo', 'bar']

– juanpa.arrivillaga
Nov 21 '18 at 21:14












1 Answer
1






active

oldest

votes


















0














The answer to the question with explanation can be found here.



For my problem specifically, I should have imported the module 1 by stating import Import_data instead of from Import_data import * and then using Import_data.jobs_db to access the variable.






share|improve this answer























    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%2f53419884%2faccess-numpy-array-vs-python-array-from-another-module%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









    0














    The answer to the question with explanation can be found here.



    For my problem specifically, I should have imported the module 1 by stating import Import_data instead of from Import_data import * and then using Import_data.jobs_db to access the variable.






    share|improve this answer




























      0














      The answer to the question with explanation can be found here.



      For my problem specifically, I should have imported the module 1 by stating import Import_data instead of from Import_data import * and then using Import_data.jobs_db to access the variable.






      share|improve this answer


























        0












        0








        0







        The answer to the question with explanation can be found here.



        For my problem specifically, I should have imported the module 1 by stating import Import_data instead of from Import_data import * and then using Import_data.jobs_db to access the variable.






        share|improve this answer













        The answer to the question with explanation can be found here.



        For my problem specifically, I should have imported the module 1 by stating import Import_data instead of from Import_data import * and then using Import_data.jobs_db to access the variable.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 15:07









        HendrikHendrik

        6513




        6513
































            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%2f53419884%2faccess-numpy-array-vs-python-array-from-another-module%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory