简体   繁体   中英

Where does the %load filename.py command look in Jupyter notebooks?

I am trying to load a .py file into a single cell in a jupyter notebook. I tried using the command %load training.py and I received the error:

 "ValueError: 'training.py' was not found in history, as a file, url, nor in the user namespace." 

I have a folder that contains all of the .py files that I want to be able to load into jupyter in C:\\Users\\Jeffrey\\CharmTagger . I then moved one of the .py files out of the folder and into C:\\Users\\Jeffrey and the %load command found that file. Is there anyway I can get jupyter to look in folders? I don't want to clutter my User folder. Is there anywhere else I can put these files so that they can still be found by %load ?

You can always use the full path:

%load C:\Users\Jeffrey\CharmTagger\training.py

When you type:

%load training.py

IPython looks into the current working directory. You can find it with:

%pwd

The magic expression %load loads files relative to notebook current working directory. The working directory for every new Jupyter notebook is set once started the notebook server. It's the same directory from where you started the server (if didn't specified a different one with the proper option jupyter notebook --notebook-dir=some_directory ).

You can get or change the working directory of your notebook with some system magics (special notebook functions) respectively:

  • %pwd ( print working directory )
  • %cd some_folder/some_other_folder ( change directory )

Let say you just started the notebook, and your current directory is your home directory and thus the output of %pwd will be something like /home/some_user .

If you want to load files from a specific folder, ie /home/some_user/my_code (or if you are using Windows C:\\Users\\Some_user\\my_code ) you can type (each in a separate notebook cell):

%cd my_code

and then:

%load my_script1.py
%load my_script2.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM