Reputation: 261
I have two scripts (written in Python 3), let's call them A.py
and B.py
, which I execute at different times. Both scripts rely on the same set of constants I defined, but I will vary their values from time to time. It's clunky though to keep updating the constants in both A.py
and B.py
.
Therefore, I'm curious if it is possible to create one script in which I can define the constants used by A.py
and B.py
, and choose to execute either A.py
or B.py
.
My failed attempt:
I tried importing A.py
and B.py
as modules into the script where the constants were defined and running the relevant script from there, but the constants were not found in the namespaces of A.py
and B.py
when imported, so it didn't work.
Upvotes: 0
Views: 1253
Reputation: 77942
You're making it much more complicated than it has to be... Just define your constants in a distinct file (let's name it "constants.py") and import it in your A.py and B.py scripts. You just have to make sure that "constants.py"i is accessible in your sys.path
(the modules search path), but this will be the case if you have all three files in the same directory.
Upvotes: 1