Python shell execution programatically - python

I have a requirement to run python commands at run on shell programmatically, its almost replicating REPL but programmatically, I tried below code which worked for the first line but it's not carrying the session the way CLI does, kindly help
import subprocess as s
import sys
res=s.run([sys.executable, "-c", "a=5"])
s.run([sys.executable, "-c", "print(a)"])
Error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
I am getting the error as those 2 commands are being executed in 2 different processes, is there any way to run in one process but in different lines(Similar to what we do in python interpreter(REPL)), I am working on the requirement to capture python commands from some external files and run them on the shell, so I won't know what command I will be executed until it actually appears in an external file.
[![enter image description here][2]][2]

You can use Popen of subprocess. stdin is waiting your commands.
import subprocess as s
import sys
res=s.Popen(sys.executable, stdin=s.PIPE)
res.stdin.write(b"a=5\n")
res.stdin.write(b"print(a)")

Related

Running python file within a shell script : module not found error

I'm beginner to both bash and python.
I'm working in Ubuntu env.
to be short, I've created a shell script 'format_data.sh' that runs python file 'proc_ko.py' within it.
#!/bin/bash
...
python path/to/python/file/proc_ko.py
...
And the python file 'proc_ko.py' imports a module called khaiii
from khaiii import KhaiiiApi
api = KhaiiiApi()
...
But when I try to execute 'format_data.sh', I get this import error from python file.
Traceback (most recent call last):
File "media/sf_projet/pe/pe/PROGRAMME/SCRIPTS/proc_ko.py", line 5, in
from khaiii import KhaiiiApi
ImportError: No module named khaiii
which doesn't occur when I execute python file independently.
Python file 'proc_ko.py' itself doesn't have any error and 'khaiii' is well installed.
so I don't understand why import error occurs only through the shell script.
If u need more details to figure out, I'll be happy to provide. Thanks in advance for help.

When calling a python script via the terminal, ImportError

When calling a python script from within Pycharm my script runs successfully. However when I call the same script via my terminal I get an import error:
Macs-MacBook:src macuser$ python ./run_events.py
Traceback (most recent call last):
File "./run_events.py", line 3, in <module>
from functions import return_ga_data
File "/Users/macuser/PycharmProjects/ops-google-extract/src/functions.py", line 2, in <module>
import connect
File "/Users/macuser/PycharmProjects/ops-google-extract/src/connect.py", line 4, in <module>
from oauth2client.service_account import ServiceAccountCredentials
ImportError: cannot import name 'ServiceAccountCredentials'
I am not using an environment. Also I'm using python 3.7.
All my python scripts are in the same directory. My terminal's pwd is the same directory.
Tried:
Tried calling the script with python3 ./run_events.py but I get the same result.
Per an SO post about paths I added this to the top of connect.py:
import sys
sys.path.append('/Users/macuser/PycharmProjects/ops-google-extract/src/functions.py')
I still got the same result.
Why can I run the file without an import error from within my IDE but not via the terminal when using ./run_events.py?
Do you have python 2 installed as well? Type python --version in the terminal and see what you get? My guess is Pycharm might be configured to use python 3 while your default python in terminal is python 2, so your python 2 is lacking those modules that was installed for python 3. So when you execute your script in the terminal it's using python 2. If that's the case you can try,
py -3 ./myscript.py

Exiting interactive python3 session from script

I'd like my program to automatically exit if it detects an error when loading a file and parsing it (even when called from an interactive mode with -i). I've tried variations of exit() and sys.exit(), but nothing seems to be working. Instead of exiting the interactive session I get a stack trace. For example, if I have the file test.py that is the following:
import sys
sys.exit(0)
when I run python3 -i test.py I get the following result:
Traceback (most recent call last):
File "test.py", line 2, in <module>
sys.exit()
SystemExit
>>>
and the session continues on, until I exit myself (using those exact lines subsequently work, just not when they're called from the script). What am I missing?
Try calling os._exit() to exit directly, without throwing an exception
import os
os._exit(1)
Note that this will bypass all of the python shutdown logic.
Hope it helps.

Windows Error 2 when running batch file in Python script

I saw several threads dedicated to this error, but none solved my problem. Thought I'd post so folks can look at my code and maybe we can figure out another solution for everyone.
I'm attempting to run a Python script whose first task is to run a batch file. The batch file actually runs Wget to download the files that the Python script will work on.
If I run the entire Python script manually, it works perfectly. However, if I run it using Windows Task Scheduler or from Command Line, it has issues with the batch script.
If I comment out the batch script part, Task Scheduler/CMD can run the Python script just fine. Task Scheduler/CMD can also run the batch file independently with no issue.
Here's my Python code:
import time
import os
import sys
from subprocess import Popen
import zipfile
import win32com.client as win32
#1# Run the downloader batch file
p = Popen("NDICDownloader.bat", cwd=r"C:\NDICUpdate")
stdout, stderr = p.communicate()
p.wait()
Here's the error I get in command line:
c:\Python27\python.exe c:\NDICUpdate\NDICUpdater.py
Traceback (most recent call last):
file "c:\NDICUpdate\NDICUpdater.py", line 9, in (module)
p = Popen("NDICDownloader.bat", cwd=r"C:\NDICUpdate")
file "c:\Python27\lib\subprocess.py", line 672, in __init__
errread, errwrite)
file "c:\Python27\lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Here's the batch file:
cd C:\NDICUpdate\NDIC
wget --arguments include sensitive info--
The batch file uses Wget to download files to the NDIC folder. All scripts are located in the root folder C:\NDICUpdate. All files exist.
The problem is trying to use Windows to run the batch file within the Python script. Why are Windows and Python butting heads here??
(Answered in the Comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
#MC ND wrote:
Change your code to call cmd.exe as the started process with /c NDICUpdate.bat as parameters to the executable:
p = Popen(["cmd.exe", "/c NDICDownloader.bat"], cwd=r"C:\NDICUpdate")
The OP wrote:
that worked. This also works: os.system("c:\windows\system32\cmd.exe /c C:\NDICDownloader.bat"). What I ended up doing in the end was writing a batch file that runs the Wget and then runs the Python. Lots of headache/wasted time, but at least a workable solution. Still no idea how to get cmd.exe to run Python's subprocess.

Run a Python script from Python prompt such that variables are loaded into the interactive environment

Say I have a (somewhat pointless) Python script
#!/usr/bin/python
a = 5
Is there a way to run this script from the interactive prompt such that after running if I type a I get
>>> a
5
and not
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
More generally, if my script calculates something through a series of steps, is there a way to access that something after the script has finished (in the same kind of manner).
Import it:
from yourscriptname import a
Each and every .py file in python is a module, and you can simply import it. If the file is called foo.py, import foo.

Categories