-1

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Megan Caithlyn
  • 374
  • 2
  • 11
  • 33
  • Note: the fact that you're using Git's submodules is not really relevant; you'd have exactly the same situation with Mercurial subrepos. – torek Jul 04 '21 at 08:28

2 Answers2

0

I've ended up making a class called Version specifically for this purpose. There is a version.py file in common subfolder.

class Version():

    PROJECT_NAME : str = "Project Name"
    PROJECT_VERSION : str = "0.00"

In project1.py:

from version import Version as v
v.PROJECT_NAME = "Project 1"
v.PROJECT_VERSION = "1.00"

In project2.py:

from version import Version as v
v.PROJECT_NAME = "Project 2"
v.PROJECT_VERSION = "1.01"

In common\functions.py and everywhere where it's necessary:

from version import Version as v

def function():
    print(v.PROJECT_NAME, v.PROJECT_VERSION)

The output of function() now depends on what project it is being run from. The trick is not to create an instance of Version and just redefine its class variables in the main file of the project.

Megan Caithlyn
  • 374
  • 2
  • 11
  • 33
-1

I guess project 1 and project 2 are two independent programs or even a set of programs each. Then you could work like this:

  1. Define a variable PROJECT_NAME in your "functions" module. E.g. set it no None. This is just to sattisfy the code checker and has no other meaning.

  2. Right after the import statement of import functions add another line like this:

    import functions

    functions.PROJECT_NAME='project1'

This sets the variable in the module. In fact you wouldn't even have to define PROJECT_NAME in the submodule, but IDEs like Spyder will mark code using variable PROJECT_NAME suspicious otherwise.

  • That will not work, because the structure is not as simple as shown, there are plenty of different common classes, which are shared between multiple projects. Therefore I'll need to set PROJECT_NAME for every particular file individually instead of having them import PROJECT_NAME themselves. – Megan Caithlyn Jul 04 '21 at 14:10