0

So, I have the following files: main.py, a1.py, a2.py and b1.py, b2.py.

Depending on whether I want to run the "type a" version of the code (of main.py) or the "type b" version, I import a1.py, a2.py or I import b1.py, b2.py.

For instance, see these lines of code below. Depending on whether the user inputs "type a" or "type b" for variable "code_type," the main.py code will import the a files or the b files.

QUESTION: Is there another way to run a certain version of the main.py code from the command prompt (rather than having to create 2 different main.py files, one for type a and one for type b). Maybe something like %python main.py type_a or %python main.py type_b to import the type a or type b files?

It's just I rather not have to go into the main.py file to edit the "code_type" variable every time.

Thanks!

if code_type=='type a':
    from a1 import *
    from a2 import *
elif code_type=='type b':
    from b1 import *
    from b2 import *
else:
    print("Please enter 'type a' or 'type b' for variable 'code_type'")
    sys.exit()
    
martineau
  • 119,623
  • 25
  • 170
  • 301
carsof
  • 83
  • 1
  • 8
  • Take a look at [importlib](https://docs.python.org/3/library/importlib.html), you will be able to import your modules dynamically by path provided by user. – Monsieur Merso Jul 29 '21 at 22:16
  • 2
    Does this answer your question? [How do I access command line arguments?](https://stackoverflow.com/questions/4033723/how-do-i-access-command-line-arguments) – Woodford Jul 29 '21 at 22:35
  • decouple your codebase, see what are the dependency you are using in the codebase for type a, type b. in seperate python file, import the them accordingly based on the type and in your main code, import those required module from newly created import fike – sahasrara62 Jul 29 '21 at 22:37

2 Answers2

0

What you are looking for is argv.

you can get arguments when the code is executed as such:

from sys import argv

print(argv[1])

If you run the code as below:

$ python main.py first

your output would be:

first

So basically argv is a list with n element. Element 0 is the file name you executed, element 1 is 1st argument, element 2 is 2nd and so on.

Some tips:

I can imagine you wrote your code in such way it will run if you import everything from a1 or b1 and a2 or b2. However the from something import * is not a good idea. You might override some builtin function or keywords. A better way would be:

if code_type=='type a':
    import a1 as first_mod
    import a2 as secnods_mod
elif code_type=='type b':
    import b1 as first_mod
    import b2 as secod_mod
else:
    print("Please enter 'type a' or 'type b' for variable 'code_type'")
    sys.exit()

And in your code you can reach everything using first_mod and second_mod.

Example:

first_mod.my_func()
MSH
  • 1,743
  • 2
  • 14
  • 22
0

You can retrieve arguments from the sys.argv list as described in the Python tutorial in the documentation.

And as @Monsieur Merso suggested in a comment, you can dynamically import the desired modules by using the importlib module.

Here's how to do those things:

import importlib
import sys

try:
    code_type = sys.argv[1]
except IndexError:
    print('Required code type argument missing.')
    code_type = None

if code_type == 'type_a':
    module_names = 'a1', 'a2'
elif code_type == 'type_b':
    module_names = 'b1', 'b2'
else:
    print('Please specify an argument of either "type_a" or "type_b"')
    sys.exit()

# Import everything public from the choosen modules.
# ("from module import *" equivalent)
for module_name in module_names:
    module = importlib.import_module(module_name)
    module_dict = module.__dict__
    try:
        to_import = module.__all__
    except AttributeError:
        to_import = [name for name in module_dict if not name.startswith('_')]
    globals().update({name: module_dict[name] for name in to_import})
martineau
  • 119,623
  • 25
  • 170
  • 301