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([])
Related
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.
I am trying to run the following code in Atom's Hydrogen package
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
which produces the following error
ipykernel_launcher.py: error: unrecognized arguments: -f
Any ideas why this might be happening and how to fix it?
Seem to be related to this issue.
Simple workaround is running import sys; sys.argv=['']; del sys;.
My configuration:
ubuntu 16.04 eclipse 4.7 python 2.7.12 PyDev 6.2.0.2017
When running test.py which contains blow code from PyDev:
print os.environ['PATH']
Expected values got printed out,then change 'PATH' to 'LIB_ROOT'
print os.environ['LIB_ROOT']
Got error:
LIB_ROOT=os.environ['LIB_ROOT']
File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'LIB_ROOT'
And same variable can be accessed from ipython console
and run test.py in bash
python test.py
expected valued got printed out also,can anybody tell me why?
I'm trying to run pylint using subprocess but getting a vague message stating non-zero exit status 28. I can not find any reference to an exit status 28 for either Pylint or subprocess.
I can only assume this is a pylint/windows issue as running some other typical commands e.g. head, works just fine as does running pylint directly in the console.
Running python 3.5 on Windows 10.
Any ideas?
MWE
import subprocess
cmd = 'pylint test_script.py'
subprocComplete = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(subprocComplete.stdout.decode('utf-8'))
Output
python35 pylint_subprocess_test.py
Traceback (most recent call last):
File "pylint_subprocess_test.py", line 3, in <module>
subprocComplete = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "C:\Python35\lib\subprocess.py", line 708, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'pylint test_script.py' returned non-zero exit status 28
If the subprocess returns nonzero (which pylint often will), then subprocComplete never gets assigned to. You can catch the error, and the error object holds the output you want.
import subprocess
cmd = 'pylint test_script.py'
try:
subprocComplete = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(subprocComplete.stdout.decode('utf-8'))
except subprocess.CalledProcessError as err:
print(err.output.decode('utf-8'))
I'm using the execnet package to allow communication between Python scripts interpreted by different Python interpreters.
The following code (test_execnet.py):
import execnet
for python_version in ('python', 'python3'):
try:
gw = execnet.makegateway("popen//python="+python_version)
ch = gw.remote_exec('channel.send(1/3)')
res = ch.receive()
print(python_version, ': ', res, sep ="")
except:
print('problems with ', python_version)
Runs perfectly in the command-line Terminal, showing the following output:
$ python3 test_execnet.py
python: 0
python3: 0.333333333333
However, if I try to run the same code from within the Eclipse IDE, I get the following error:
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 4, in <module>
File "<string>", line 2, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages/execnet/gateway_base.py", line 8, in <module>
import sys, os, weakref
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/os.py", line 380, in <module>
from _abcoll import MutableMapping # Can't use collections (bootstrap)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/_abcoll.py", line 54
class Hashable(metaclass=ABCMeta):
^
SyntaxError: invalid syntax
problems with python
problems with python3
NOTE:
Eclipse Version: 3.6.0
PyDev Interpreter configured for the project: python3
"Preferences/Interpreter - Python"'s Python Interpreters:
python (/usr/bin/python)
python3 (/Library/Frameworks/Python.Framework/Versions/3.1/Resources/Python.app/Contents/MacOS/Python
EDIT:
I write a code to show the os.environ like this:
for python_version in ('python', 'python3'):
try:
import os
for item in os.environ:
print(item, '= ', os.environ[item])
except:
print('problems with ', python_version)
I got the following outputs:
eclipse_output.txt
terminal_output.txt
A FileMerge comparison of the files can be found at eclipse_output.txt vs. terminal_output.pdf.
Any hints?
Thanks
seems like pydev does site-customizations and particularly modifies things for interactive/console usage (judging from a very quick skim of http://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev/pysrc/pydev_sitecustomize/sitecustomize.py ). This is not useful or fitting for execnet-mediated processes.
You could try to "del os.environ['PYTHONPATH']" before you invoke execnet.makegateway, or, to be more careful, just delete the sitecustomize part of it.
hth,
holger
'import site' failed; use -v for traceback
I have seen that when python was unable to find its landmark. Which that indicates there is a PYTHONHOME problem.
Check out http://docs.python.org/using/cmdline.html#envvar-PYTHONHOME maybe eclipse is screwing your environment up.
Edit:
Looked at your env dumps, looks like eclipse is definitely messing with PYTHONPATH, which will cause your child python processes to not work correctly.
Basically what you have going on here is eclipse starts a python v2 instance with a PYTHONPATH pointing to the python v2 directories. Then you spawn a python v3 process which tries to load its landmark from the python v2 directories...
You need to find a way to have eclipse not mess with the PYTHONPATH. I am not sure what eclipse is trying to do by doing that, but it is certainly no friend when you want to spawn new python processes.