1

I'm fairly new to python and I've been exclusively using Jupyter Notebooks. When I need to run a .py file I have saved somewhere in my computer what I normally do is just use the magic command %run

%run '/home/cody/.../Chapter 3/efld.py'
%run '/home/cody/.../Chapter 5/tan_vec.py'

Then in the next cell I can run efld.py without a problem. But tan_vec.py uses efld.py and looks like this,

def tan_vec(r):
    import numpy as np
    #Finds the tangent vector to the electric field at point 'r'
    e = efld(r)
    e = np.array(e) #Turn 'e' into a numpy array so I can do math with it a little easier.
    emag = np.sqrt(sum(e**2)) #Magnitude of the 
    return e / emag

when I try to run that I get the error;

"NameError: name 'efld' is not defined."

I tried most of the things here but none of them seamed to work.

Am I going about running py files in the notebook wrong? Is there a better way to run/call py files in a notebook? How do make it so that I can run one py file inside another py file?

EDIT

Thank you everyone for your help! I just wanted to add the final code that worked in case anyone comes across this later and wants to see what i did.

def tan_vec(r):
    #import sys
    #sys.path.insert(0, '/home/cody/Physics 331/Textbook Programs/Chapter 3')
    from efld import efld 
    import numpy as np
    #Finds the tangent vector to the electric field at point 'r'
    e = efld(r)
    e = np.array(e) #Turn 'e' into a numpy array so I can do math with it a little easier.
    emag = np.sqrt(sum(e**2)) #Magnitude of the 
    return e / emag

The first two lines are commented out because they were only needed if efld.py and tan_vec.py are saved in different folders. I just added a copy of efld to the same folder and tan_vec and I didn't need them any more.

Thank you again for all the help!

CodyBrown
  • 13
  • 4
  • Sounds like you should import efld at the top of tan_vec.... – Shadow Jun 12 '18 at 05:06
  • @CodyBrown If there are errors in the imported script, the script will not be imported! – Naren Murali Jun 12 '18 at 05:06
  • @Shadow That was one of the things suggested in the link. I put import efld at the beginning of tan_vec but then i get the error, "TypeError: 'module' object is not callable" – CodyBrown Jun 12 '18 at 05:10
  • You want to import the function - not the module. So it'd look more like `from tan_vec import tan_vec` – Shadow Jun 12 '18 at 05:15
  • @CodyBrown It's better not to put the answer to your question in your question itself. Instead, post it as an answer if you don't find the existing answer satisfactory. – David Z Jun 12 '18 at 07:42
  • @DavidZ I'll keep that in mind next time. Still new to stack so I'm still learning the normal edict. – CodyBrown Jun 12 '18 at 15:01

1 Answers1

2

put the files in the jupyter root directory. then just import those files (now called modules) at the top of your first cell:

from efld import *
from tan_vec import *

If one requires the other, put the import on top inside the respective file rather than to jupyter.

Given that those modules don't throw any exceptions within itself, you can then call all functions in it in all other cells.

e = efld(r)

Pay attention that all functions within both files are named differently.


Edit: As pointed out in the comments below, you can also import your functions directly:

from efld import efld as <whatever>

In this way, you can rename your functions to <whatever> and don't have to rename the ones with identical names, sitting in different modules/files.

sudonym
  • 3,788
  • 4
  • 36
  • 61
  • 1
    `Pay attention that all functions within both files are named differently.` - that problem goes away if you import the specific things you need instead of wildcarding... – Shadow Jun 12 '18 at 05:23