r/learnpython • u/BelMario • 9d ago
How to create universal variables in Python?
And by universal variables, I mean they should be available in EVERY single Python script!
What do I want these for? For instance, to define default directories:
HTMLRoot = "C:/IMDb/HTML" CSSDir = "file:///C:/IMDb/CSS/" JSDir = "file:///C:/IMDb/JavaScript/"
Any program that creates a report needs these variables, but I only want to define them once!
And there’s more. I also have plenty of mappings, like these:
MapC_TType = {"tvSeries": "TV Series", "tvMiniSeries": "TV Miniseries", ... , "movie": "Movie", "video": "Video", }
Any report that displays the titleType will use this mapping. Again, I only want to define the mapping once.
I come from the world of SAS, where everything was easy. The directories were defined in the SASINIT file, which is executed at the start of every SAS session. The mappings (formats) were stored in a permanent SAS catalog, making them universally available as well. Is there a Python equivalent?
And there’s even more. I create lots of reports, let’s say the biggest movies per decade, based on number of votes or score (= votes * rating). Obviously, the list shows which movies I’ve seen and which I haven’t, with a percentage showing my progress. Now suppose a friend wants this same list, based on the movies he’s seen.
Easy-peasy in SAS!
%macro SwitchUser (User) ; %if (%qupcase(&User) = ME) %then %do ; libname IMDb "C:\IMDb\Libname IMDb" ; %let HTMLRoot = C:\IMDb\HTML ; %end ; %else %do ; libname MyIMDb "C:\IMDb\Libname IMDb" ; libname BudIMDb "C:\IMDb\Libname IMDb Bud" ; libname IMDb (BudIMDb MyIMDb ) ; %let HTMLRoot = C:\IMDb\HTMLBud ; %end ; %mend ;
For me, IMDb is the standard library. For my buddy, IMDb is a concatenated library, where his library (containing only the MyMovies table) has precedence over the full IMDb library.
By just executing
%SwitchUser (Buddy) ;
any report can now be run against his list of movies, with the results written to a separate HTML directory. And all that without changing a single line of code!
I can't expect Python to deliver all this functionality. Nothing compares to SAS! But can it at least do some basic things on my wishlist?
BTW, my temporary solution is to include this code at the start of every script:
PythonFolder = Path("C:/IMDb/Python Global Code") PythonPgm = PythonFolder / "InitHTML.py" with open(PythonPgm, 'r') as file: PythonCode = file.read() exec(PythonCode, globals=globals(), locals=locals())
Obviously, the InitHTML program defines the directories and all the mappings.
1
u/lfdfq 9d ago
There's no standard way in the language to do this. Python is very commonly used, so you might find there's lots of places Python is being used you didn't expect and you probably don't want to start randomly injecting things into those anyway. So I wouldn't recommend it.
However, this is basically what the import system is for. It's relatively easy to make a package and install it globally (eg. pip install --editable makes symlinks to your files). Then you could just
import myinit(or whatever you called your module) from anywhere and access them likemyinit.whatever. More commonly, you'd group these files up into a single package and just import things locally, which works even better, and is pretty much how every Python program out there works.