how to access a variable from a .py script in another .py script
I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!
first.py
def main():
results = os.path.abspath(r'D:results')
return 0
second.py
-> need to access results here
Any help highly appreciated?
python
add a comment |
I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!
first.py
def main():
results = os.path.abspath(r'D:results')
return 0
second.py
-> need to access results here
Any help highly appreciated?
python
1
Something like this, but bear in mind you are returning0, notresult_path-from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).
– Peter
Nov 12 '18 at 11:57
resultsis only known within the scope ofmain(), you cannot access it from anywhere else, not even fromfirst.py.
– Mike Scotty
Nov 12 '18 at 11:58
You might want to start using pickle that allows you to save and load python objects to/from disk
– jojo
Nov 12 '18 at 12:06
add a comment |
I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!
first.py
def main():
results = os.path.abspath(r'D:results')
return 0
second.py
-> need to access results here
Any help highly appreciated?
python
I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!
first.py
def main():
results = os.path.abspath(r'D:results')
return 0
second.py
-> need to access results here
Any help highly appreciated?
python
python
edited Nov 12 '18 at 12:00
Mike Scotty
5,53751933
5,53751933
asked Nov 12 '18 at 11:55
Remya ReveendranRemya Reveendran
11
11
1
Something like this, but bear in mind you are returning0, notresult_path-from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).
– Peter
Nov 12 '18 at 11:57
resultsis only known within the scope ofmain(), you cannot access it from anywhere else, not even fromfirst.py.
– Mike Scotty
Nov 12 '18 at 11:58
You might want to start using pickle that allows you to save and load python objects to/from disk
– jojo
Nov 12 '18 at 12:06
add a comment |
1
Something like this, but bear in mind you are returning0, notresult_path-from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).
– Peter
Nov 12 '18 at 11:57
resultsis only known within the scope ofmain(), you cannot access it from anywhere else, not even fromfirst.py.
– Mike Scotty
Nov 12 '18 at 11:58
You might want to start using pickle that allows you to save and load python objects to/from disk
– jojo
Nov 12 '18 at 12:06
1
1
Something like this, but bear in mind you are returning
0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).– Peter
Nov 12 '18 at 11:57
Something like this, but bear in mind you are returning
0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).– Peter
Nov 12 '18 at 11:57
results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.– Mike Scotty
Nov 12 '18 at 11:58
results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.– Mike Scotty
Nov 12 '18 at 11:58
You might want to start using pickle that allows you to save and load python objects to/from disk
– jojo
Nov 12 '18 at 12:06
You might want to start using pickle that allows you to save and load python objects to/from disk
– jojo
Nov 12 '18 at 12:06
add a comment |
4 Answers
4
active
oldest
votes
You will need a function, which will then be imported, like:
from example.py import func()
Hope this helps.
add a comment |
In the file second.py
import first as f
main = f.main()
result = main.result_path
Please try this. I hope it helps a bit.
1
mainwill be the int value0, so you're trying to access0.result_path, which will fail.
– Mike Scotty
Nov 12 '18 at 12:05
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
add a comment |
You need to import your first script in your second and additionally set result_path as a global variable.
first.py:
result_path = None
def main():
global result_path
result_path = os.path.abspath(r'D:results')
second.py:
import first
access = first.result_path
You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
add a comment |
This will work
First.py
def main():
results = os.path.abspath(r'D:results')
return results
second.py
from First import main
resultsHere = main()
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%2f53261653%2fhow-to-access-a-variable-from-a-py-script-in-another-py-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You will need a function, which will then be imported, like:
from example.py import func()
Hope this helps.
add a comment |
You will need a function, which will then be imported, like:
from example.py import func()
Hope this helps.
add a comment |
You will need a function, which will then be imported, like:
from example.py import func()
Hope this helps.
You will need a function, which will then be imported, like:
from example.py import func()
Hope this helps.
answered Nov 12 '18 at 11:58
mgmc2mgmc2
63
63
add a comment |
add a comment |
In the file second.py
import first as f
main = f.main()
result = main.result_path
Please try this. I hope it helps a bit.
1
mainwill be the int value0, so you're trying to access0.result_path, which will fail.
– Mike Scotty
Nov 12 '18 at 12:05
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
add a comment |
In the file second.py
import first as f
main = f.main()
result = main.result_path
Please try this. I hope it helps a bit.
1
mainwill be the int value0, so you're trying to access0.result_path, which will fail.
– Mike Scotty
Nov 12 '18 at 12:05
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
add a comment |
In the file second.py
import first as f
main = f.main()
result = main.result_path
Please try this. I hope it helps a bit.
In the file second.py
import first as f
main = f.main()
result = main.result_path
Please try this. I hope it helps a bit.
answered Nov 12 '18 at 12:02
Alex_PAlex_P
193114
193114
1
mainwill be the int value0, so you're trying to access0.result_path, which will fail.
– Mike Scotty
Nov 12 '18 at 12:05
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
add a comment |
1
mainwill be the int value0, so you're trying to access0.result_path, which will fail.
– Mike Scotty
Nov 12 '18 at 12:05
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
1
1
main will be the int value 0, so you're trying to access 0.result_path, which will fail.– Mike Scotty
Nov 12 '18 at 12:05
main will be the int value 0, so you're trying to access 0.result_path, which will fail.– Mike Scotty
Nov 12 '18 at 12:05
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.
– Alex_P
Nov 12 '18 at 12:14
add a comment |
You need to import your first script in your second and additionally set result_path as a global variable.
first.py:
result_path = None
def main():
global result_path
result_path = os.path.abspath(r'D:results')
second.py:
import first
access = first.result_path
You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
add a comment |
You need to import your first script in your second and additionally set result_path as a global variable.
first.py:
result_path = None
def main():
global result_path
result_path = os.path.abspath(r'D:results')
second.py:
import first
access = first.result_path
You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
add a comment |
You need to import your first script in your second and additionally set result_path as a global variable.
first.py:
result_path = None
def main():
global result_path
result_path = os.path.abspath(r'D:results')
second.py:
import first
access = first.result_path
You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.
You need to import your first script in your second and additionally set result_path as a global variable.
first.py:
result_path = None
def main():
global result_path
result_path = os.path.abspath(r'D:results')
second.py:
import first
access = first.result_path
You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.
answered Nov 12 '18 at 12:07
AracKnightAracKnight
387
387
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
add a comment |
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()
– Remya Reveendran
Nov 12 '18 at 12:47
add a comment |
This will work
First.py
def main():
results = os.path.abspath(r'D:results')
return results
second.py
from First import main
resultsHere = main()
add a comment |
This will work
First.py
def main():
results = os.path.abspath(r'D:results')
return results
second.py
from First import main
resultsHere = main()
add a comment |
This will work
First.py
def main():
results = os.path.abspath(r'D:results')
return results
second.py
from First import main
resultsHere = main()
This will work
First.py
def main():
results = os.path.abspath(r'D:results')
return results
second.py
from First import main
resultsHere = main()
answered Nov 12 '18 at 12:07
HayatHayat
526415
526415
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%2f53261653%2fhow-to-access-a-variable-from-a-py-script-in-another-py-script%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
1
Something like this, but bear in mind you are returning
0, notresult_path-from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).– Peter
Nov 12 '18 at 11:57
resultsis only known within the scope ofmain(), you cannot access it from anywhere else, not even fromfirst.py.– Mike Scotty
Nov 12 '18 at 11:58
You might want to start using pickle that allows you to save and load python objects to/from disk
– jojo
Nov 12 '18 at 12:06