Jupyter Import. Py Error [Closed]

I am trying to import a .py file that I have in jupyter notebook.

import torch
from torch import Tensor
import dlc_practical_prologue as prologue

And this is the error I got. dlc_practical_prologue.py is in the same directory. I am using a virtual box.

usage: ipykernel_launcher.py [-h] [--full] [--tiny] [--force_cpu]
                             [--seed SEED] [--cifar] [--data_dir DATA_DIR]
ipykernel_launcher.py: error: unrecognized arguments: -f /run/user/1000/jupyter/kernel-3640175e-3052-4216-9c84-6be6f65474e4.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


/home/dave/miniconda3/lib/python3.6/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)

You can't import a file with argparse in global namespace.

For example, you may have following codes in dlc_practical_prologue.py:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--seed', default=1, type=int)
opt = parser.parse_args()
train(opt)

You should move the codes under an if statement:

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--seed', default=1, type=int)
    opt = parser.parse_args()
    train(opt)
James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest