I have a large project, which has grown to have its derivatives.
The main project and its derivatives are split into different repositories with a submodule called common
.
The structure looks like this (oversimplified):
project1.git
submodule "common"
project1.py
project2.git
submodule "common"
project2.py
common.git
functions.py
In some cases I need functions inside common
to be aware of the project name and version, for instance to access a project-specific subfolder in %APPDATA%
.
**project1.py**
PROJECT_NAME = "Project 1"
PROJECT_VERSION = "1.00"
from common import functions
**project2.py**
PROJECT_NAME = "Project 2"
PROJECT_VERSION = "1.01"
**common\functions.py**
from __main__ import PROJECT_NAME
import winreg
def getAppDataFolder():
return os.path.join(winreg.ExpandEnvironmentStrings("%appdata%"), PROJECT_NAME)
Of course I can pass around PROJECT_NAME
variable in every function possible, but it is very verbose and inconvenient.
One of the ways to get rid of from __main__ import PROJECT_NAME
is to use a constants.py
file inside the common
submodule, which will detect PROJECT_NAME
from sys.argv[0]
, but I don't like this solution, because I don't want the common
submodule to have the names of all of my projects hardcoded inside of it.
I'm OK with a placeholder for PROJECT_NAME
, but it needs to be declared once in the **project1.py**
main script.
How do I import PROJECT_NAME
correctly so it becomes a global variable for the submodule?
Edit: The person who closed the question clearly didn't understand it, because I wasn't asking about general importing of variables between files. This is much more specific - having variables with the same name in different projects using common parts of code, but the trick is to have the variables declared outside of the common code.