maciek2791 maciek2791 - 4 months ago 188
Python Question

Run sqoop in python script

I'm trying to run sqoop command inside Python script. I had no problem to do that trough shell command, but when I'm trying to execute python stript:

#!/usr/bin/python
sqoopcom="sqoop import --direct --connect abcd --username abc --P --query "queryname" "

exec (sqoopcom)


I got an error, Invalid syntax, how to solve it ?

Answer

The build in exec statement that you're using is for interpreting python code inside a python program.

What you want is to execute an external (shell) command. For that you could use call from the subprocess module

import subprocess
subprocess.call(["echo", "Hello", "World"])

https://docs.python.org/3/library/subprocess.html