How to save classes and functions for universal usage in python - python

I want save some classes and functions that I have wrote for Neural Networks, which I want to use in the future whenever I need them. Is there a way to save functions and classes in some library?
So more precisely, I'm looking for a library (let's call it tools), so that I can do:
save my_function in tool
...
from tool import my_function

The way to do that in Python is to simply save your functions in a separate python file (also called a module, see the official docs).
In your case the custom function code could be saved as the file tool.py.
You can then use the syntax you mentioned:
from tool import my_function
To import this specific function, but only if the file tool.py is actually in the same directory as the session you are importing it to (this is an easy way to add the module to your Module Search path, see the offical documentation).
If you want to use the module in another directory, you can append the path where you saved tool.py to your sys.paths:
import sys
sys.path.append('/usr/dir/customcode/')
Then you can from tool import my_function in the same session, if you have tool.py saved in the directory /usr/dir/customcode/.

Related

Including other Python file in current project

I have the following file. It contains several functions that I would use frequently. Instead of writing the same function over and over in every project, I think it would be more efficient if we are able to include the files in each project.
I want to include functions inside this file:
However, I don't know what technique is called and I am not sure if it is possible to make it work.
tools is not found:
Here's the folder that I want to include:
I have created __init__.py as explained in https://stackoverflow.com/a/4116384/17869806
If you are using a Jupyter notebook, create a new one in the folder ivm_github_python with this content:
from tools import ivm_string_print
ivm_string_print.myfunction()
in this case myfunction is defined in ivm_string_print.py
So in the file which is the main file for each project you can do
import filename and import it as a module in the start of the project.
Note: the file which you want to import should be in the same folder as your main file. and suppose the file name you want to import is "functions.py"
when you import it write import functions and not import functions.py
pls upvote if helps :)

Run a function when importing module in Python

What I want to achieve: I have a function (object/module/etc) that allows me to run a model. This model requires a few parameters, so I want to have them in a config file rather than pass all through the code. So what I want is to have a config file created automatically when my module is imported. Is it possible in python? Can I have some pointers on where to start please?
All the code in a python file is run when its imported. If you have
def f():
print("ran")
print("imported")
Then when you import it it will print imported
This is also why you sometimes see if __name__=="__main__":
In some files. The code in that block is only run if the file is run as main not imported.
However creating files in a predetermined location may be bad UX, so if you want other people to use your library id think of a better solution.
Generally you can just put code at top level of a module and it will run.

When I import a package in my python script, does Python also import the sub package?

I am doing an online Python course and currently learning about modules and packages, but can't seem to get my head around the logic. I have used modules (or maybe they were packages who knows?) before in current job but without really knowing what was happening when I would do import Pandas for example.
By following the course I have created a folder called "MyMainPackage" and in this folder there is another folder called MySubPackage.
In "MyMainPackage":
init.py and my_main_script.py; in my_main_script.py there is a function called main_func()
in "MySubPackage":
init.py and my_sub_script.py; in my_sub_script.py there is a function called sub_func()
In Sublime Text editor I wrote a script called myprogram2.py:
import MyMainPackage
MyMainPackage.my_main_script.main_func()
However, this hasn't worked; when I call python myprogram2.py in Windows Command Prompt I get the following message
Attribute Error: module MyMainPackage has no attribute my_main_script
However, as per the online lecture, what does work is this:
from MyMainPackage import my_main_script
from MyMainPackage.MySubPackage import my_sub_script
my_main_script.main_func()
my_sub_script.sub_func()
Why can't I just import the entire package and access the modules like I have tried to above instead of the way the online lecture does it? I thought it would be the same thing. I am just struggling to understand the logic.

Converting Python scripts to APIs

I have a Python script that extracts certain consumer product aspects from customer reviews using LinearSVC, but I am trying to convert this script into some sort of API to use for new reviews. Is there an easy way to do this? I am very new to the whole concept of APIs.
An API is just a library you import once it's reachable by the interpreter in your case. So any import in python is you calling on an library/API.
So if you're script is called foobar.py for example, if it is in the same directory as other python files using
import foobar
at the top of your python file should allow you to reference any functions made in your original python script.

Python project architecture

I'm a java developer new to python. In java, you can access all classes in the same directory without having to import them.
I am trying to achieve the same behavior in python. Is this possible?
I've tried various solutions, for example by importing everything in a file which I import everywhere. That works, but I have to type myClass = rootFolder.folder2.folder3.MyClass() each time I want to access a foreign class.
Could you show me an example for how a python architecture over several directories works? Do you really have to import all the classes you need in each file?
Imagine that I'm writing a web framework. Will the users of the framework have to import everything they need in their files?
Put everything into a folder (doesn't matter the name), and make sure that that folder has a file named __init__.py (the file can be empty).
Then you can add the following line to the top of your code:
from myfolder import *
That should give you access to everything defined in that folder without needing to give the prefix each time.
You can also have multiple depths of folders like this:
from folder1.folder2 import *
Let me know if this is what you were looking for.

Categories