Azure App Function in Python - Include Classes - python-3.x

When working with Azure App Functions inside of Visual Studio Code I would like to add python Classes to import into init.py by including their files?
Is there a recommended way to accomplish this?

I will show how to import the classes under test file outside HttpTrigger1 function, classes under test file inside HttpTrigger1 function.
Here is my file structure, :
Import the class named Display in init.py under test file inside HttpTrigger1 function:
from test import Display
Import the class named Display1 in yes.py under test file inside HttpTrigger1 function:
from test.yes import Display1
Import the class named Display in init.py under test file outside HttpTrigger1 function:
from ..test import Display
Import the class named Display1 in yes.py under test file outside HttpTrigger1 function:
from ..test.yes import Display1

Related

Dynamically importing identically named classes from files in subfolder

I need to to dynamically import identically named classes from files in a subfolder. The classes have different implementations in every file. After the import I need to create an instance of each class and store them in a data structure so I can invoke each function in thee instances.
folder structure:
./main.py
./players/player1.py
./players/player2.py
./players/player3.py
./players/player4.py
player1.py
class PlayerClass():
def doStuff():
stuff implementation 1
player2.py
class PlayerClass():
def doStuff():
stuff implementation 2
So main.py will import all PlayerClasses from these files and create once instance each to then be able to call functions like doStuff form each instance.
Something like:
importedClasses[0].doStuff()
I have managed to extract file names but I can't get the import to work from a subfolder and I can't import them as unique objects and store them in a list or similar either.
Here is one approach
from glob import glob
from importlib.machinery import SourceFileLoader
modules = sorted(glob('players/player*.py'))
imported_classes = list(map(lambda pathname: SourceFileLoader(".", pathname).load_module().PlayerClass(), modules))
imported_classes[0].doStuff()
Here we are using glob to match the pathnames to the modules we would like to import. Then import them using the importlib module (SourceFileLoader).

Python import from class in different file

Here is my code in the main file:
from File2.Test import test
test()
And here is the code for the file containing the class:
class Test:
def test(self):
print('Test')
As you can see, I don't just want to import the class, I want to import something from the class. When I try the syntax above, I get this error: ModuleNotFoundError: No module named 'File2.Test'; 'File2' is not a package. If there is anyway to just import test() from File2, please let me know. Any help would be appreciated!
from File2.Test import test
Python interprets this as you asking for a function called test from a file called Test.py in a directory called File2 (which also happens to have an __init_.py), thinking that File2 is a package.
Rather, you are trying to import a class method from a different file. The preferred way of calling such a method is to import the class definition and the call the method fromt eh class:
from File2 import Test
test = Test.test
Now, you have a way of calling test. However, as long as test is not static, you'll run into issues in actually calling it

Import statement breaks in class-containing module when importing the class from different files

Here's my directory structure:
app
-Folder1
-class-container.py
-queries.py
-script.py
-Folder2
-Folder3
-main.py
In my class-container file I import the SQL queries from queries.py with from Folder1.queries import sql_queries
When I import and use the class in main.py everything runs smoothly. But when I do the same from script.py I get a ModuleNotFoundError... no module named Folder1. I've tried relative imports and changing it to from queries import sql_queries but every time I change it, it breaks one or the other. Any insight would be valuable. Thanks!

questions about importing in python 3

i've seen plenty of importing questions but didn't find any that explained importing very "easily". there are are 3 types of importing that i know of, and none of them seem to do what i'm looking for or would like to do. eg. main.py has def a() def b() class c() and def d() and lets say i have a start.py file.
main:
def a():
print("1")
def b():
print("2")
class c():
def__init__(self,name = "Rick")
self.name = name
def d():
print("4")
so now im my start.py file i want to import everything from them. what is the best way? i have tried using import main and i run into issues after creating an instance of class c [ ricky = c() ]that ricky isn't defined or accessing ricky.name it will say module ricky has no attribute name. so that doesn't seem to work. what is this even used for if you aren't importing the entire main.py file?
then there is from main import a, b, c, d that seems to work just fine, but there really has to be another way than having to import every single function, definition, variable, and everything.
third there is from main import * i'm not sure how this one works, i have read some on it mainly stating there should be an __ all __ = everything i want imported. but where do i put this. at the very top of the page inside my main.py? but there still should be a better way?
is my import main just not working correctly? or do i have to list everything i want to import either in a from main import statement or in an __ all __ list?
does importing carry over to another py file? eg. 1.py 2.py 3.py if inside 2.py i import 3.py correctly and everything works. in 1.py can i just import 2.py and it will import 3.py into 1.py from the import statement inside of 2.py? or do i have to import 2.py and 3.py again into 1.py?
the 3 main imports:
import (pythonfile) aka "module" using this will import all classes and functions to be used. does not import other imports. to call something in the imported module eg. modlue: MAIN, function: FUNC ... to call: MAIN.FUNC()
from module import FUNC, CLASS, .... when using this import you don't need to call it with the module. it is almost as if it is right infront of you eg.
module: MAIN, function: FUNC ..... to call: FUNC()
from module import * a combination of the previous two imports. will import everything from the module to be accessed without calling with the module extention. this form imports other imports as well. so if you have two modules that need to talk to eachother. using this will cause an error since you will be trying to import a module into another module then back into it's self again. A imported into B, A and B imported back into A. it doesn't work. so watch when using. May cause other importing errors of trying to import multiple modules that share imports. eg. importing A and B into C if A and B share D (pending for testing)
from MAIN import * call function: FUNC()
hope this helps other people out who are having issues understanding exactly how importing works and calling your functions/classes and what not.

python confusion on importing modules

I saw this statement
from django import forms
the folder structure is
django\
__init__.py
forms\
__init__.py
..(continues)
My doubt is instead of the above statement why cant we imports forms like this.
import django.forms
when i tried this in pycharm. it says unused import statement.
the following is my code:
#from django import forms
import django.forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'text',)
it gives me error
class PostForm(forms.ModelForm):
NameError: name 'forms' is not defined
From python's docs,
... when using syntax like import item.subitem.subsubitem, each item
except for the last must be a package; the last item can be a module
or a package but can’t be a class or function or variable defined in
the previous item.
Then since its __init__.py designates forms as a package, you should be able to import it via import django.forms. Accessing its members will be different since where before you wrote forms.x now you would write django.forms.x.

Resources