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 :)
Related
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.
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/.
I have a project where I want to structure the code in layers. The different parts of the program do very different things, and I wish to have a clean upper layer which binds all of the code in sub-directories together.
However, I struggle with importing modules correctly.
Say I have the structure
Project
└──manage.py
└──part a
├──script_a.py
├──__init__.py
└──modules_a
├──module_a1.py
├──module_a2.py
├──module_a3.py
└──__init__.py
└──part b
├──script_b.py
├──__init__.py
└──modules_b
├──module_b1.py
├──module_b2.py
├──module_b3.py
└──__init__.py
If I am writing code in script_a.py that depends on something from module_a1.py I use
from modules_a import module_a1
This works, but VS Code is never happy about the importing, always marking the imports with error. Therefore, I am wondering if there is something that I have logically misunderstood, especially since the script_a.py is not in the root folder?
If you are within a package and you want to access a sub package you have to put a . in front of the sub package. So change your import statement from
from modules_a import module_a1
to
from .modules_a import module_a1
Then the error disappears.
I decided to solve it by adding a testing file in the root folder and only running the script from the testing file, which will have similar functionality to the manage.py that will be my execution script later.
I am making a web project, for this i have summery scripts and crawling scripts now I want to link both these two scripts together so that my both scripts work automatically for a single project.
For example: I am using PyCharm.
If i have one file is Summery.Py and second file is crawler.Py both have separate modules. I want to link them a one program
You could try import to make the functions in one script callable in the other.
E.g. Within crawler.py add import Summery and call the functions within normally.
This is handled through the import functionality of Python. You can do something like:
crawler.py
from Summery import * # will run Summery.py and load all global variables and functions
Or use the more apt
from Summery import myFunction # get just one function
It works both ways too
Summery.py
from crawler import * # you don't include the '.py'
If they are in different directories, you do
from MainPyCharmFolder.SubFolder.DeepFolder.Summery import myFunction
Which enables you to navigate your folder structure
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.