Python Forum
Sharing global variables between script in python
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sharing global variables between script in python
#1
Hello guys,

I have just moved from Matlab to Python (I am using Spyder environment) and I am depressed that I cannot share the global variables between script. I would like to have in the same folder one file, where all functions are defined, including parameters (global variables) and to use those functions in every script in the folder, where are the parameters defined. So I would like to have something like this:
-----------------------------------------
file: funs.py:

def my_fun(x):
   return(x + A)
-----------------------------------------
file: my_script1.py:

import funs

A=5
print(funs.my_fun(3))
#this will return me result 8
-----------------------------------------
file: my_script2.py:

import funs

A=6
print(funs.my_fun(3))
#this will return me result
-----------------------------------------

in Matlab I simply defined in every function that A is global and I dind't have to define its value in the functions file. It took the appropriate value from each script, where A was also defined as global. However in Python it is not enough, since it always display an error that A is not defined. I want to have the A to be different for every script, where I call the function, but I don't want to have A as variable, but parameter instead.

Any suggestions or advises?

Thanks a lot in advance!

correction:

-----------------------------------------
file: my_script2.py:

import funs

A=6
print(funs.my_fun(3))
#this will return me result 9
-----------------------------------------
Reply


Messages In This Thread
Sharing global variables between script in python - by capponero - Oct-27-2016, 08:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to secure API key when sharing quarinteen 2 399 Jan-19-2024, 04:46 PM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,468 Jun-29-2023, 11:57 AM
Last Post: gologica
  Trying to understand global variables 357mag 5 1,203 May-12-2023, 04:16 PM
Last Post: deanhystad
  Why Pip is not listed in the official Global Python Module index? quazirfan 2 808 Apr-21-2023, 10:55 AM
Last Post: snippsat
  Global variables or local accessible caslor 4 1,083 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,199 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 994 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  How to output Python in Spout or any other video sharing network? buzzdarkyear 4 2,242 Jan-11-2022, 11:37 AM
Last Post: buzzdarkyear
  How to install modules for 2.7 (or sharing from 3.8)) Persisto 2 2,516 Dec-31-2021, 02:33 PM
Last Post: Persisto
  multiprocessing and sharing object cyrduf 0 2,087 Feb-02-2021, 08:16 PM
Last Post: cyrduf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020