PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

[FIXED] How to save a python script into a local directory and call it with an import statement?

 August 24, 2022     module, python     No comments   

Issue

I am brand new to creating modules. I am working with a large Jupyter Lab (JL) file with lots of custom functions. I would like to take those functions, save them in a python file in the same working directory that my JL file is pulling data from, and import them as functions. I don't want to save them to the PIP package manager just yet; I just want to save them as local packages for now.

The functions would be in a .py file in a format similar to this:

def func1(x,y,z):
    """
    x = custom input
    y = custom imput
    z = custom input
    """
    
    do stuff


def func2(x,y,z):
    """
    x = custom input
    y = custom imput
    z = custom input
    """
    
    do stuff

def func3(x,y,z):
    """
    x = custom input
    y = custom imput
    z = custom input
    """
    
    do stuff

def func4(x,y,z):
    """
    x = custom input
    y = custom imput
    z = custom input
    """
    
    do stuff

I would save this in a python file such as "custom_func.py" and save it in the same directory as my JL file.

After saving the python file, what do I need to do so that I can import this as a custom local package? I have seen that you need to do something with __init__.py, but I'm just not sure what that is.


Solution

After saving the python file, what do I need to do so that I can import this as a custom local package?

You don't have to do anything else. You can bring the function into your namespace from the script "custom_func.py" into the JupyterLab notebook.
Did you try:

from custom_func import func1, func2, func3, func4

Now you can use each of those functions in your notebook as if they had been written in your notebook. For example, result = func4(5,6,7).

Or alternatively, you can bring in the entire custom_func script and call it by reference to the name of the imported file followed by a dot followed by the function, like module_name.function_name:

import custom_func
my_result = custom_func.func1(1,2,3)

I just adapted something like here to your example. You may want to look over that post as it may give you other options you prefer. The same things works in a script as well, which is what that post was about.


One of the reasons you may want to limit what you bring in is so you don't clobber what you have in your namespace already. Imagine you already had a func3 defined. If you brought in a func3 by the first method I suggested, you'd probably note that was going on and act accordingly. Maybe rename your other function. If you did from custom_func import *, you might not notice you just replaced func3. If you import the second way I suggested, the new functions are under the module name reference, and so that helps eliminate there being two func3. Because one will just be func3 and one is custom_func.func3. However, often the first option I proposed is preferable so that you keep your namespace as clean as possible.



Answered By - Wayne
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing