Writing notes when running a normal python executable (file containing argparse) with Jupyter notebook

Normally https://github.com/pfnet/chainer/blob/master/examples/mnist/train_mnist.py When I run the sample python file with jupyter, I get the following error.

usage: __main__.py [-h] [--batchsize BATCHSIZE] [--epoch EPOCH] [--gpu GPU]
                   [--out OUT] [--resume RESUME] [--unit UNIT]
__main__.py: error: unrecognized arguments: -f /run/user/2049/jupyter/kernel-84d4df02-bc59-4802-9d99-c3228bb4e71f.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


/home/mil/watanabe/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/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)

In this way, an error occurs around args.

In such a case, put "easydict"

pip install easydict

You can try it quickly by changing it as follows.

sample.py


# parser = argparse.ArgumentParser(description='Chainer example: MNIST')
# parser.add_argument('--batchsize', '-b', type=int, default=100,
#                     help='Number of images in each mini-batch')
# parser.add_argument('--epoch', '-e', type=int, default=20,
#                     help='Number of sweeps over the dataset to train')
# parser.add_argument('--gpu', '-g', type=int, default=-1,
#                     help='GPU ID (negative value indicates CPU)')
# parser.add_argument('--out', '-o', default='result',
#                     help='Directory to output the result')
# parser.add_argument('--resume', '-r', default='',
#                     help='Resume the training from snapshot')
# parser.add_argument('--unit', '-u', type=int, default=1000,
#                     help='Number of units')
# args = parser.parse_args()
import easydict
args = easydict.EasyDict({
        "batchsize": 100,
        "epoch": 20,
        "gpu": 0,
        "out": "result",
        "resume": False,
        "unit": 1000
})

Recommended Posts

Writing notes when running a normal python executable (file containing argparse) with Jupyter notebook
Trap trapped when running a Python Windows executable
Try running Python with Try Jupyter
How to batch start a python program created with Jupyter notebook
Make a sound with Jupyter notebook
When writing a program in Python
Creating a simple PowerPoint file with Python
Linking python and JavaScript with jupyter notebook
I made a configuration file with Python
Freeze with send_keys of file selection when running Selenium WebDriver in Python [PhantomJS]
A note I was addicted to when running Python with Visual Studio Code
Error when installing a module with Python pip
How to read a CSV file with Python 2/3
When Html cannot be output with Jupyter Notebook
Read a file containing garbled lines in Python
Create a GUI executable file created with tkinter
CRLF becomes LF when reading a Python file
I get a UnicodeDecodeError when running with mod_wsgi
A memo when creating a python environment with miniconda
Create a Photoshop format file (.psd) with python
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
A story when a Python user passes a JSON file
File overwrite confirmation with option that takes a file object as an argument with Python argparse
A memo that uses an interactive display mode like Jupyter notebook with VSCode + Python
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
Convert jupyter notebook .ipynb files to python executable .py files
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
I tried running python etc. from a bat file
A memorandum when writing experimental code ~ Logging in python
Drawing a tree structure with D3.js in Jupyter Notebook
Problems when creating a csv-json conversion tool with python
What's in that variable (when running a Python script)
From buying a computer to running a program with python
When writing an if statement with a regular expression
Current directory when creating a new one with Jupyter
Run a Python file with relative import in PyCharm
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a 2d CAD file ".dxf" with python [ezdxf]
Directory structure when writing tests with Python 3 standard unittest
Are you still using Jupyter Notebook? Enjoy a comfortable Python life with Jupyter Life (.py) with VS Code ?!