What is this 'stdin' argument in subprocess? - subprocess

from subprocess import *
run(args='cat', stdin='testfile.txt')
What does stdin do in the subprocess module in python ?

Related

IPython - Hide traceback when SystemExit is raised

I'm surprised no one asked this issue before, but somehow I couldn't find an answer.
When raising SystemExit in a simple my_script.py.
import sys
sys.exit(2)
No traceback is displayed when running python my_script.py, as explained in python doc:
SystemExit: When it is not handled, the Python interpreter exits; no stack traceback is printed.
However when running with ipython, a traceback is printed:
$ ipython my_script.py
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
my_script.py in <module>
2 import sys
3
----> 4 sys.exit(2)
SystemExit: 2
This is an issue when using argparse as parser.parse_args() call sys.exit() if parsing fail. So user see an unhelpful SystemExit: 2 rather than the error message printed above the traceback.
A workaround is to use os._exit but this feels very hacky and might not properly cleanup.
Is there a ipython flag which would silence/hide the traceback when SystemExit is raised, like with standard python interpreter ?
Here's a sample session with xmode 'plain':
In [1]: import argparse
In [2]: parser=argparse.ArgumentParser()
In [3]: parser.add_argument('foo')
Out[3]: _StoreAction(option_strings=[], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
In [4]: import sys; sys.argv
Out[4]:
['/usr/local/bin/ipython3',
'--pylab',
'qt',
'--nosep',
'--term-title',
'--InteractiveShellApp.pylab_import_all=False',
'--TerminalInteractiveShell.xmode=Plain']
In [5]: parser.parse_args()
usage: ipython3 [-h] foo
ipython3: error: unrecognized arguments: --pylab --nosep --term-title --InteractiveShellApp.pylab_import_all=False --TerminalInteractiveShell.xmode=Plain
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py:3426: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
In [6]: parser.parse_args([])
usage: ipython3 [-h] foo
ipython3: error: the following arguments are required: foo
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py:3426: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
This shows the error message produced by the parser, followed by the capture and traceback. This allows me to continue interactively.
With plain interpreter, I get the message followed by an exit.
2310:~/mypy$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
>>> parser=argparse.ArgumentParser()
>>> parser.add_argument('foo');
_StoreAction(option_strings=[], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args()
usage: [-h] foo
: error: the following arguments are required: foo
2311:~/mypy$
For me the whole purpose of using ipython is to try multiple things, without it quitting on me. Otherwise I'd run the script without the interactive layer.

Python import pdfplumber error " ModuleNotFoundError: No module named 'chardet' "

I encountered an error while importing pdfplumber in Python3, indicating module chardet is missing. However, running pip list from the cmd confirms that the package is installed, version 3.0.4. Anyone had similar experience? Any resolution?
Error Message:
~\AppData\Roaming\Python\Python37\site-packages\pdfminer\utils.py in <module>
3 """
4 import struct
----> 5 import chardet # For str encoding detection
6
7 # from sys import maxint as INF doesn't work anymore under Python3, but PDF
ModuleNotFoundError: No module named 'chardet'
Error: No module named chardet

How to solve unrecognized arguments error?

The problem is that I can not get through the first step of running the simplest command. When I write this code
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
an then i get this error
usage: main.py [-h]
main.py: error: unrecognized arguments: -f
C:\Users\Saeid\AppData\Roaming\jupyter\runtime\kernel-301e1312-128e-4c4d-9ae8-
035b05a69a59.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\Program Files\Anaconda3\lib\site-
packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use
'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
The argparse module is used to parse command line arguments. Hence, it doesn't make much sense to do so in an IPython or Jupyter notebook. The error propably stems from the fact that the notebook was invoked with an -f option.
As funky-future pointed out, you should not be using an IPython notebook with argparse. To test how it works, let's assume a file named test.py with the following content:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)
Now in your terminal (cmd or PowerShell) you go to the directory with the test.py file and type:
python test.py 123
The output should be:
123
Source
To avoid that error message, you can do
import argparse
parser = argparse.ArgumentParser()
parser.parse_args([])

unregistered task type import errors in celery

I'm having headaches with getting celery to work with my folder structure. Note I am using virtualenv but it should not matter.
cive /
celery_app.py
__init__.py
venv
framework /
tasks.py
__init__.py
civeAPI /
files tasks.py need
cive is my root project folder.
celery_app.py:
from __future__ import absolute_import
from celery import Celery
app = Celery('cive',
broker='amqp://',
backend='amqp://',
include=['cive.framework.tasks'])
# Optional configuration, see the application user guide.
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
app.start()
tasks.py (simplified)
from __future__ import absolute_import
#import other things
#append syspaths
from cive.celery_app import app
#app.task(ignore_result=False)
def start(X):
# do things
def output(X):
# output files
def main():
for d in Ds:
m = []
m.append( start.delay(X) )
output( [n.get() for n in m] )
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
I then start workers via (outside root cive dir)
celery -A cive worker --app=cive.celery_app:app -l info
which seems to work fine, loading the workers and showing
[tasks]
. cive.framework.tasks.start_sessions
But when I try to run my tasks.py via another terminal:
python tasks.py
I get the error:
Traceback (most recent call last):
File "tasks.py", line 29, in <module>
from cive.celery_app import app
ImportError: No module named cive.celery_app
If I rename the import to:
from celery_app import app #without the cive.celery_app
I can eventually start the script but celery returns error:
Received unregistered task of type 'cive.start_sessions'
I think there's something wrong with my imports or config but I can't say what.
So this was a python package problem, not particularly a celery issue. I found the solution by looking at How to fix "Attempted relative import in non-package" even with __init__.py .
I've never even thought about this before, but I wasn't running python in package mode. The solution is cd'ing out of your root project directory, then running python as a package (note there is no .py after tasks):
python -m cive.framework.tasks
Now when I run the celery task everything works.

pyql is not accessable after its installation from native for Ubuntu14.04 python2.7

After installing pyql from the package source(according to its own wizard described in this file) on Ubuntu 14.04 into /usr/local/lib/python2.7/dist-packages/ folder, all tests finished with success. This folder includes all additional installed packages, which are accessible, from the python using native import command.
But, this specific installation python don’t see, and I cannot import anything from it.
Have you any idea, what I need to define in addition?
Thanks,
Yigal B.
There are results of my sys.path:
import sys
print(sys.path)
['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
python -c 'from quantlib.settings import __quantlib_version__; print __quantlib_version__'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named quantlib.settings
Can you post the content of you sys.path? And the output of
python -c 'from quantlib.settings import __quantlib_version__; print __quantlib_version__'