Reputation: 13
I have the file1 here:
#File1
class MyClass1():
def abc(self)
---
def efg(self)
---
and here's the file2:
#File2
from File1 import MyClass1
def test1()
callfromfile1 = Myclass1()
callfromfile1.abc()
def test2()
callfromfile1 = Myclass1()
callfromfile1.efg()
if __name__== "__main__":
test()
Prob:
How to call test2
method only in terminal/command prompt?
note:
1. I'm using python3
2. Should I need to add another "class (eg. MyClass2)" above in file2
in order to call the test2
specicifically?
3. Please give some example for reference.
Upvotes: 0
Views: 56
Reputation: 189749
If file2
is actually called file2.py
and is in your PYTHONPATH
you can say
python3 -c 'import file2; test2()'
If not, maybe try
(cat file2; echo 'test2()') | python3
A third possible solution is to make the last clause more complex.
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
test()
elif 'test2' in sys.argv[1:]:
test2()
# maybe more cases here in the future
and call it like
python3 file2 test2
to take the elif
branch.
Upvotes: 1