Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import python script with arguments

I have scripts:

moving1.py:

def move():
    print("walk!")

moving2.py:

def move():
    print("run!")

And man.py, that can accept by argument parameter moving1 or moving2 script to act.

man.py:

import sys

if len(sys.argv) <= 1:
    exit("Too less arguments calling script")

__import__(sys.argv[1])
moving = sys.modules[sys.argv[1]]

def move():
    moving.move()

Now I have testman.py script, that have to test all variants of man.py execution:

testman.py

import man #and somehow add here as argument "moving1"
man.move()

import man #and somehow add here as argument "moving2"
man.move()

There exist a lot of similar questions, but they don't do exactly what I want. How can I add arguments to imported scripts? Problem is not to check

if __name__ = "__main__":

there, problem is to import script exactly with parameters I want. Is it possible?

like image 405
Arkady Avatar asked Jun 24 '14 08:06

Arkady


People also ask

How do you add arguments to a Python script?

To add arguments to Python scripts, you will have to use a built-in module named “argparse”. As the name suggests, it parses command line arguments used while launching a Python script or application. These parsed arguments are also checked by the “argparse” module to ensure that they are of proper “type”.

How do you pass arguments in a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

How do you give a Python script a command line input?

If so, you'll need to use the input() command. The input() command allows you to require a user to enter a string or number while a program is running. The input() method replaced the old raw_input() method that existed in Python v2. Open a terminal and run the python command to access Python.


2 Answers

You should separate your argument handling code and your import code:

man.py

import sys

def move():
    moving.move()

def setup(module):
    global moving
    moving = __import__(module)

if __name__ == "__main__":
    if len(sys.argv) <= 1:
        exit("Too less arguments calling script")

    setup(sys.argv[1])

testman.py

import man
man.setup(<name>)
man.move()

However, this seems like a very odd way of acheiving what you are trying do do. Perhaps you could clarify your goals?

like image 74
Jamie Cockburn Avatar answered Oct 10 '22 01:10

Jamie Cockburn


If you are taking filename as command line argument and if you want to import it, then use imp load_source module.

import imp
module = imp.load_source("module.name", sys.argv[1])
#Then call the function
module.move()

Basically imp module helps to load the modules in run-time.

Hope this helps!

like image 39
user2109788 Avatar answered Oct 10 '22 02:10

user2109788