How to conditionally import and run a method from another Python package?
up vote
0
down vote
favorite
I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:
suite=multiplication python3 .
Here's my current file structure:
__main__.py
suites/
__init__.py
addition.py
subtraction.py
multiplication.py
division.py
suites/__init__.py
__all__ = ['addition', 'subtraction', 'multiplication', 'division']
subtraction.py
def testSuite():
# Bunch of tests
__main__.py
import os
import suites
# Get suite name from 'suite=xxx' in command line
suiteName = os.getenv('suite')
# Based on suiteName, load the correct file
suite = suites[suiteName]
# Call the suite loaded from the file
suite()
This errors out:
suite = suites[suiteName]
TypeError: 'module' object is not subscriptable
What's the best way to conditionally import and run a script from another package?
python
add a comment |
up vote
0
down vote
favorite
I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:
suite=multiplication python3 .
Here's my current file structure:
__main__.py
suites/
__init__.py
addition.py
subtraction.py
multiplication.py
division.py
suites/__init__.py
__all__ = ['addition', 'subtraction', 'multiplication', 'division']
subtraction.py
def testSuite():
# Bunch of tests
__main__.py
import os
import suites
# Get suite name from 'suite=xxx' in command line
suiteName = os.getenv('suite')
# Based on suiteName, load the correct file
suite = suites[suiteName]
# Call the suite loaded from the file
suite()
This errors out:
suite = suites[suiteName]
TypeError: 'module' object is not subscriptable
What's the best way to conditionally import and run a script from another package?
python
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:
suite=multiplication python3 .
Here's my current file structure:
__main__.py
suites/
__init__.py
addition.py
subtraction.py
multiplication.py
division.py
suites/__init__.py
__all__ = ['addition', 'subtraction', 'multiplication', 'division']
subtraction.py
def testSuite():
# Bunch of tests
__main__.py
import os
import suites
# Get suite name from 'suite=xxx' in command line
suiteName = os.getenv('suite')
# Based on suiteName, load the correct file
suite = suites[suiteName]
# Call the suite loaded from the file
suite()
This errors out:
suite = suites[suiteName]
TypeError: 'module' object is not subscriptable
What's the best way to conditionally import and run a script from another package?
python
I have a project that contains several test suites. I want to be able to specify which suite I'd like to run in the command line:
suite=multiplication python3 .
Here's my current file structure:
__main__.py
suites/
__init__.py
addition.py
subtraction.py
multiplication.py
division.py
suites/__init__.py
__all__ = ['addition', 'subtraction', 'multiplication', 'division']
subtraction.py
def testSuite():
# Bunch of tests
__main__.py
import os
import suites
# Get suite name from 'suite=xxx' in command line
suiteName = os.getenv('suite')
# Based on suiteName, load the correct file
suite = suites[suiteName]
# Call the suite loaded from the file
suite()
This errors out:
suite = suites[suiteName]
TypeError: 'module' object is not subscriptable
What's the best way to conditionally import and run a script from another package?
python
python
asked Nov 9 at 17:47
RobertAKARobin
1,6731223
1,6731223
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
Use importlib.import_module
:
from importlib import import_module
suite = import_module('suites.' + suiteName)
suite.testSuite()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Use importlib.import_module
:
from importlib import import_module
suite = import_module('suites.' + suiteName)
suite.testSuite()
add a comment |
up vote
4
down vote
Use importlib.import_module
:
from importlib import import_module
suite = import_module('suites.' + suiteName)
suite.testSuite()
add a comment |
up vote
4
down vote
up vote
4
down vote
Use importlib.import_module
:
from importlib import import_module
suite = import_module('suites.' + suiteName)
suite.testSuite()
Use importlib.import_module
:
from importlib import import_module
suite = import_module('suites.' + suiteName)
suite.testSuite()
answered Nov 9 at 17:52
jwodder
31.9k34877
31.9k34877
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230898%2fhow-to-conditionally-import-and-run-a-method-from-another-python-package%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown