Apologies if this has been asked before; it seems to be a bit of a recurring theme, but I'm keen to learn whether there is a better way to handle this situation than I am currently using.
I am trying to identify the cleanest way to handle importing modules in my Azure Python function. Currently this is what I have to do:
import logging
import sys
import pathlib
import os
parent = str(pathlib.Path(__file__).parent) + '\.venv\Lib\site-packages'
sys.path.append(parent)
import azure.functions as func
If I don't then I get the dreaded import error for azure.functions module.
I have taken to putting my .venv INSIDE the function dir then making sure that .venv is added to the .funcignore so it does not get uploaded.
My project structure looks like this:
So the parent var is getting me up to the TimerTrigger1 then I append the .venv\Lib\site-packages then add that to the path so that the import of azure.functions does not fail.
My question: Is there a better way?
I am afraid you should do append to let Python know where your modules reside so it can correctly import them into your scripts for use.
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'yourenv/Lib/site-packages')))
Related
I want to import main_file.py from sub_file.py, i have my init files setup. I am able to do in my main_file.py:
from sub_folder.sub_file import *
However i do not know how to do it the other way around.
This is my structure:
|+main_folder
|--_init_.py
|--main_file.py
|++sub_folder
|---_init_.py
|---sub_file.py
The old way:
Make sure the directory with main_folder is on your sys.path;
from main_folder import main_file.
The new, usually better way:
from ..main_folder import main_file
This has the advantage of never clashing with system imports. If you rename your main_folder to e.g. math, from math import my_func will crash, because stdlib's math does not have this function, or import from your module, depending on sys.path. OTOH from ..math import my_func will definitely always import from your own module.
If in doubt, always print sys.path before your failing import statement to understand if you're actually looking at the right directories.
I have set of inbuilt functions in 'pythonfile1.py' located at '/Users/testuser/Documents', the file contains
import os
import sys
import time
Now i want to import 'pythonfile1.py' to 'pythonfile2.py', which is located at '/Users/testuser/Documents/execute'
I have tried with my following code and it didn't work:
import sys
sys.path[0:0] = '/Users/testuser/Documents'
import pythonfile1.py
print os.getcwd()
I want it to print the current working directory
Your question is a bit unclear. Basically, there are two things 'wrong'.
First, your import statement is broken:
import pythonfile1.py
This specifies a file name, not a module name - modules don't contain dots and extensions. This is important because dots indicate sub-modules of packages. Your statement is trying to import module py from package pythonfile1. Change it to
import pythonfile1
Second, there's no need to fetch builtins from another module. You can just import them again.
# pythonfile1
import os
print 'pythonfile1', os.getcwd() # assuming py2 syntax
# pythonfile2
import os
print 'pythonfile2', os.getcwd()
If you really want to use os from pythonfile1, you can do so:
# pythonfile2
import os
import pythonfile1
print 'pythonfile2', os.getcwd()
print 'pythonfile1->2', pythonfile1.os.getcwd()
Note that os and pythonfile1.os in pythonfile2 are the exact same module.
if you want to import stuff from another file, you should use python modules.
If you will create file named init.py then the execute folder becomes a module.
After that you can use
from .pythonfile1 import function_name
or you can use
from .pythonfile1 import *
which imports all, but better solution is name everything you want to use explicitly
you can find more about modules in documentation
Why do we need to import module1.module2 if we can just import module1?
Example:
Why do we need import tkinter.messagebox and do tkinter.messagebox.askyesno(“blah text”) when we also can do import os and still can do os.path.join(“/“, “blah”)?
I use import os in my code regularly, and I saw in someone else’s code the import tkinter.messagebox.
You can only use module1.module2 without an explicit import if module1 itself imports module2. For instance, os internally imports one of several other path-handling modules (depending on the OS) and calls it path. This path is then just a variable inside the os module that lets you access the os.path module.
This is one rationale. Generally when people do a
import os
more often than not, they use methods that belong to both os.path and os like os.path. abspath() and os.getcwd(). So importing os makes more sense. If you are assured you are going to use just the methods in os.path you might as well import os.path and it is perfectly fine.
Similarly, if you are sure you are gonna be using just the methods in tkinter.messagebox you do
import tkinter.messagebox
I have a directory in my Python 3.3 project called /models.
from my main.py I simply do a
from models import *
in my __init__.py:
__all__ = ["Engine","EngineModule","Finding","Mapping","Rule","RuleSet"]
from models.engine import Engine,EngineModule
from models.finding import Finding
from models.mapping import Mapping
from models.rule import Rule
from models.ruleset import RuleSet
This works great from my application.
I have a model that depends on another model, such that in my engine.py I need to import finding.py in engine.py. When I do: from finding import Finding
I get the error No Such Module exists.
How can I import class B from file A in the same module/directory?
Since you are using Python 3, which disallows these relative imports (it can lead to confusion between modules of the same name in different packages).
Use either:
from models import finding
or
import models.finding
or, probably best:
from . import finding # The . means "from the same directory as this module"
Apparently I can do: from .finding import Finding and this works.
And the answer below reflects this as well so I guess this is reasonably correct.
I've fixed up my file naming and moved my tests to a different directory and I am running smoothly now. Thanks!
Is there a place when I can put default imports for all my modules?
If you want default imports when using the python shell you can also set the PYTHONSTARTUP environmental variable to point to a python file that will be executed whenever you start the shell. Put all your default imports in this file.
Yes, just create a separate module and import it into yours.
Example:
# my_imports.py
'''Here go all of my imports'''
import sys
import functools
from contextlib import contextmanager # This is a long name, no chance to confuse it.
....
# something1.py
'''One of my project files.'''
from my_imports import *
....
# something2.py
'''Another project file.'''
from my_imports import *
....
Note that according to standard guidelines, from module import * should be avoided. If you're managing a small project with several files that need common imports, I think you'll be fine with from module import *, but it still would be a better idea to refactor your code so that different files need different imports.
So do it like this:
# something1.py
'''One of my project files. Takes care of main cycle.'''
import sys
....
# something2.py
'''Another project file. Main program logic.'''
import functools
from contextlib import contextmanager # This is a long name, no chance to confuse it.
....