Search code examples
pythonarraysnumpyglobal-variableslocal-variables

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


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.


Solution

  • 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.