2

I have the following folder Layout

-- Keywords
-- Tests
  -- MyTest.robot
-- PythonLibraries
  -- MyPythonScript.py
-- Results

When i manually navigate to the PythonLibraries folder, and run the following command, my script works and the function runs as normal, taking the argument 'Test123'.

python -c 'import MyPythonScript; MyPythonScript.My_Function();' Test123

I am now trying to introduce this into the MyTest.robot in my RobotFramework using the Process Library. I have tried using both "Run Process" and "Start Process" with no luck. I know i need to include some configuration to point the script to my PythonLibraries folder. I also need to be able to take an argument, eg. Test123.

${result}=        Start Process      python    cwd=${ROOT}/PythonLibraries   -c    'import MyPythonScript; MyPythonScript.MyFunction();' Test123

I need help with the syntax of how to code this. The support online for this is limited unless you just want to print to the terminal, which isn't much help to me. Any help with this would be greatly appreciated.

2
  • Is there a reason you want to call the Python script from the command line, instead of wrapping it in a custom library? Commented Feb 4, 2019 at 17:29
  • @A.Kootstra Yes, the python function in quiestion opens a command shell that i need to keep open as i do other UI interaction, so need to to be able to switch between process and UI. Commented Feb 5, 2019 at 11:15

1 Answer 1

2

It's important to remember that for the command on the command line, the quotes are a requirement of the shell, not python. Thus, when doing that same command line from something other than the shell, you need to use the appropriate quoting (or none at all).

In the case of robot, the quoting is the space between arguments. In other words, you don't need to use the single quotes to tell python where the command starts and ends like you do in the shell, that's what the two-or-more spaces are for.

In your case, the command to run from Start Process is "python", and the arguments to that command are "-c", the script as a whole, and "Test123". They each need to use the robot "quoting" -- spaces around each.

Finally, the keyword arguments for the keyword must come after the command to be run, and after the non-keyword arguments.

The following should work. I've used multiple lines to make it a bit easier to see the individual arguments

${result}= Start Process      
...  python     
...  -c    
...  import MyPythonScript; MyPythonScript.MyFunction() 
...  Test123  
...  cwd=${ROOT}/PythonLibraries

Another way to write it would be this:

${result}= Start Process      
...  python    -c    import MyPythonScript; MyPythonScript.MyFunction()  Test123 
...  cwd=${ROOT}/PythonLibraries
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bryan, this is exactly what i was looking for. Thanks for the clear explanation! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.