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.
python arrays numpy global-variables local-variables
|
show 3 more comments
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
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 toimport Import_data; jobs_db = Import_data.jobs_db
, but nowjobs_db
has nothing to do withImport_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, soImport_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 dojobs_db = ['foo', 'bar']
– juanpa.arrivillaga
Nov 21 '18 at 21:14
|
show 3 more comments
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
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
python arrays numpy global-variables local-variables
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 toimport Import_data; jobs_db = Import_data.jobs_db
, but nowjobs_db
has nothing to do withImport_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, soImport_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 dojobs_db = ['foo', 'bar']
– juanpa.arrivillaga
Nov 21 '18 at 21:14
|
show 3 more comments
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 toimport Import_data; jobs_db = Import_data.jobs_db
, but nowjobs_db
has nothing to do withImport_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, soImport_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 dojobs_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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 '18 at 15:07
HendrikHendrik
6513
6513
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 nowjobs_db
has nothing to do withImport_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, soImport_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