30

I am following this tensorflow tutorial after two days setting up the environment I finally could run premade_estimator.py using cmd

but when I try to run the same code in a jupyter notebook I am getting this error:

usage: ipykernel_launcher.py [-h] [--batch_size BATCH_SIZE]
                             [--train_steps TRAIN_STEPS]

ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\david\AppData\Roaming\jupyter\runtime\kernel-4faecb24-6e87-40b4-bf15-5d24520d7130.json

An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

C:\Anaconda3\envs\python3x\lib\site-packages\IPython\core\interactiveshell.py:2918: 
UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

I have tried to fix it without success using:

pip install --ignore-installed --upgrade jupyter

pip install ipykernel
python -m ipykernel install

conda install notebook ipykernel
ipython kernelspec install-self

Any idea will be appreciate! Thanks!

virtualdvid
  • 2,323
  • 3
  • 14
  • 32

9 Answers9

73

A more elegant solution would be:

args, unknown = parser.parse_known_args()

instead of

args = parser.parse_args()
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Badger Titan
  • 741
  • 1
  • 5
  • 3
  • 5
    This answer solves the args parse problem in Jupyter like a charm! But could you please explain a little bit? Thank you! – Lucecpkn Jul 12 '21 at 02:56
32

I got it! the reason why we get the error is because this code is using argparse and this module is used to write user-friendly command-line interfaces, so it seems, it has a conflict with Jupyter Notebook.

I found the solution in this page:

What we have to do is:

Delete or comment these lines:

parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=1000, type=int,
                    help='number of training steps')

and replace args

args = parser.parse_args(argv[1:])

for a dictionary using the library easydict in this way:

args = easydict.EasyDict({
    "batch_size": 100,
    "train_steps": 1000
})

With easydict we can access dict values as attributes for the arguments.

Update

After all this year diving deeper in python, I found the solution for this question is way more simple (We don't need to use any external library or method). argparse is just one of the many ways to pass arguments to a script in python from the terminal. When I tried to do it in a jupyter notebook obviously that wasn't going to work. We can just replace in the function directly the parameters like:

funtion(batch_size=100, train_steps=1000)

Now, if we have a long list of parameters for our function, we can use *args or **kargs.

*args pass a tuple as parameters in the function, for this case, in particular, it will be:

args = (100, 1000)
function(*args)

**kargs pass a dictionary as arguments to our function:

kargs = {"batch_size": 100,
        "train_steps": 1000}
function(**kargs)

just google it and you will find a really good explanation on how to use them both, here one documentation that I used to study this.

Hrvoje
  • 13,566
  • 7
  • 90
  • 104
virtualdvid
  • 2,323
  • 3
  • 14
  • 32
  • 1
    Thanks a lot! Encountered the same problem and this post helped me resolve it. – math_enthusiast Oct 30 '18 at 14:40
  • 3
    Or just use `parser.parse_args(args=[])`, because `parse_args()` will pass `_sys.argv[1:]` as `args`'s default parameter, replace it with empty list `[]` solve this problem. – allenyllee Dec 20 '18 at 13:51
20

I just ran into this problem today and found a quick, stupid solution is to insert an argument processor for the -f argument that qtconsole/ipython passes though and we did not expect. At end of parser.add_argument I put in:

parser.add_argument("-f", "--fff", help="a dummy argument to fool ipython", default="1")

I don't use the -f parameter, so there's no loss for me.

I'd rather not re-engineer a larger argument processing framework just because of ipython cropping up in workflow on one particular computer...

pauljohn32
  • 2,079
  • 21
  • 28
2

I got this problem with :

from serial.tools.list_ports import main
main()

but it's a library from serial import

so i make a copy in my own directory the file from : /usr/lib/python3/dist-packages/serial/tools

and edit to add :

parser.add_argument("-f", "--fff", help="a dummy argument to fool ipython", default="1")

as pauljohn32 (https://stackoverflow.com/users/1086346/pauljohn32) did !

now using the new file locally:

from list_ports import main
main()

it works fine !

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
jd54
  • 29
  • 1
  • similarly getting error from another library (unittest mock) when calling unittest.main(), instead setup a testsuite+runner ```suite = unittest.TestLoader().loadTestsFromTestCase(TestSen5xI2cDevice) unittest.TextTestRunner().run(suite)``` – Tyeth May 27 '23 at 17:59
2

Another solution as suggested here is passing empty string to the parse_args method.

For instance:

import argparse


def arguments():
  parser = argparse.ArgumentParser(description='Example')
  
  return parser.parse_args("")

The above solution actually works in google-colab.

Ahmet
  • 7,527
  • 3
  • 23
  • 47
1

Have you tried :

conda install ipykernel --name Python3
python -m ipykernel install
An0n
  • 705
  • 6
  • 19
1

Python's argparse module is typically used for handling command line arguments in a Python script. When you run a script from the command line, you can provide arguments that the script uses to customize its behavior. However, argparse doesn't work in a Jupyter notebook (or a Google Colab Project) because notebooks aren't run from the command line, so there are no command line arguments for argparse to parse. If you have a script that uses argparse and you want to run it in a notebook, you'll have to find a different way to provide the arguments.

I used the Namespace module like this:

from argparse import Namespace

args = Namespace(
    retriever="bm25",
    index_path="indexes/lucene-index.enwiki-20180701-paragraphs",
    k1=1.2, 
    b=0.75, 
    language="english"
)
searcher = build_searcher(args)

The argparse.Namespace class in Python is a simple 'holder' object for attributes. This class is typically used for command line argument parsing. The parsed arguments are then stored as attributes within a Namespace object.

A Namespace object can be thought of as an object where you can add attributes dynamically. It is like a Python dictionary, but instead of using the bracket notation my_dict["key"], you can use the dot notation my_namespace.key for example like this:

args = Namespace(name="Alice", age=25)
print(args.name)
print(args.age)  
Jakob
  • 21
  • 5
0

I was getting error for many more "unrecognized arguments": ipykernel_launcher: error: unrecognized arguments: --ip=127.0.0.1 --stdin=9008 --control=9006 --hb=9005 --Session.signature_scheme="hmac-sha256" --Session.key=b"2dd531d9" --shell=9007 --transport="tcp" --iopub=9009 --f=tmp-18240vZXCSGIbxxcJ.json An exception has occurred, use %tb to see the full traceback.

For each unrecognized argument (i.e ip, stdin, control, ..), I added a line to add the argument to my ArgParser (as suggested by @pauljohn32):

        ap.add_argument(
            "-i", "--ip", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-s", "--stdin", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-c", "--control", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-b", "--hb", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-K", "--Session.key", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-S", "--Session.signature_scheme", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-l", "--shell", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-t", "--transport", help="a dummy argument to fool ipython", default="1")
        ap.add_argument(
            "-o", "--iopub", help="a dummy argument to fool ipython", default="1")
Sohrab
  • 181
  • 2
  • 4
0

because this code is using argparse and this module is used to write user-friendly command-line interfaces, so it seems, it has a conflict with Jupyter Notebook.

the reason is as above, but I think it is better to convert args to class than to dict as @virtualdvid and @Hrvoje said

class args_:
 self.batch_size = 100,
 self.train_steps = 1000
args = args_
lam vu Nguyen
  • 433
  • 4
  • 9