How do I import every module and every method in project folder?
(Note: This is not production code, it's for debugging.)
Example:
How do I make this:
from views.display_hot_keys import display_hot_keys
from views.display_zones import display_zones
from controllers.api_key_controller import all_api_keys_nicknames
from models.api_data_standardizer import single_balance_standardizer
..become something like this...
from controllers.* import *
from views.* import *
from models.* import *
Edit: Perhaps I'm missing something, but I don't see how this question is answered in How can I get a list of locally installed Python modules??
Related
Let's say I have a file where I'm importing some packages:
# myfile.py
import os
import re
import pathlib
def func(x, y):
print(x, y)
If I go into another file and enter
from myfile import *
Not only does it import func, but it also imports os, re, and pathlib,
but I DO NOT want those modules to be imported when I do import *.
Why is it importing the other packages I'm importing and how do you avoid this?
The reason
Because import imports every name in the namespace. If something has a name inside the module, then it's valid to be exported.
How to avoid
First of all, you should almost never be using import *. It's almost always clearer code to either import the specific methods/variables you're trying to use (from module import func), or to import the whole module and access methods/variables via dot notation (import module; ...; module.func()).
That said, if you must use import * from module, there are a few ways to prevent certain names from being exported from module:
Names starting with _ will not be imported by import * from .... They can still be imported directly (i.e. from module import _name), but not automatically. This means you can rename your imports so that they don't get exported, e.g. import os as _os. However, this also means that your entire code in that module has to refer to the _os instead of os, so you may have to modify lots of code.
If a module contains the name __all__: List[str], then import * will export only the names contained in that list. In your example, add the line __all__ = ['func'] to your myfile.py, and then import * will only import func. See also this answer.
from myfile import func
Here is the fix :)
When you import *, you import everything from. Which includes what yu imported in the file your source.
It has actually been discussed on Medium, but for simplification, I will answer it myself.
from <module/package> import * is a way to import all the names we can get in that specific module/package. Usually, everyone doesn't actually use import * for this reason, and rather sticked with import <module>.
Python's import essentially just runs the file you point it to import (it's not quite that but close enough). So if you import a module it will also import all the things the module imports. If you want to import only specific functions within the module, try:
from myfile import func
...which would import only myfile.func() instead of the other things as well.
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')))
I have files as follow,
file1.py
file2.py
file3.py
Let's say that all three use
lib7.py
lib8.py
lib9.py
Currently each of the three files has the lines
import lib7
import lib8
import lib9
How can I setup my directory/code such that the libs are imported only once, and then shared among the three files?
You will have to import something at least once per file. But you can set it up such that this is a single import line:
The probably cleanest way is to create a folder lib, move all lib?.py in there, and add an empty file called __init__.py to it.
This way you create a package out of your lib?.py files. It can then be used like this:
import lib
lib.lib7
Depending on where you want to end up, you might also want to have some code in the __init__.py:
from lib7 import *
from lib8 import *
from lib9 import *
This way you get all symbols from the individual lib?.py in a single import lib:
import lib
lib.something_from_lib7
Import each of them in a separate module, and then import that:
lib.py:
import lib7
import lib8
import lib9
In each of the files (file1.py, file2.py, file3.py), just use import lib. Of course, you then have to reference them with lib.lib7 – to avoid that, you can use from lib import *.
import os
os.exit(0)
Instead of importing the whole module, is there any way to import the specific module in OS? (This could make my program more efficient when used.)
from os import _exit
This code should Import it specifically which should improve 'performance'.
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!