Importing Commonly Used Modules in Python - python

I usually need to import one or more identical modules to different py files, say
a.py
import sys
import os
b.py
import sys
c.py
import os
I don't want to import the same module again in different files, so I decide to write a importHelper.py and write the following
import sys
import os
So I just add import importHelper.py to a,b,c.py but outcome it does not work. (Cannot load the sys and os methods)
Is there any advice on how to import common modules on different files?
Thanks all for the reply.

It is possible with from importHelper import *. In this case you can use the same syntax, i.e. sys.exit() etc.
When you import it as import importHelper, you'll have to use it as importHelper.sys.exit().
All in all, imported modules are merely labels (variable names) and can be used as such.

Don't do that. Unnecessarily importing code into every module will get you to premature bloat. And from the Python Style Guide:
Wildcard imports (from import *) should be avoided, as they
make it unclear which names are present in the namespace, confusing
both readers and many automated tools.
As furas points out, modules are only imported once per session anyways (although you might reload one.)
And as Eric Urban says, it is convention, and expected. If you share your work with others, you will frustrate them to no end if you do this.

You should import the dependency in all files that need it. That is the python way.

Python imports module only once even if you import that module in many files. It remeber modules imported before. So you can import as many times as you wish.
You should add import module in files which use that module to make code more readable for others (and for you).

Related

How to import the module in python by overriding the builtin modules

How to import a module which we have created in python.
Example
I have created the math module in python but when I'm importing by using
import math
Then it's importing the math module from Standard libraries which I don't require
I have tried importing by giving
from my_location import math
But didn't work
So please any suggestions how to import our modules by overriding the built-in module??
You can use importlib.import_module to manually import modules, assuming your module is in your PYTHONPATH:
https://docs.python.org/3/library/importlib.html#importlib.import_module
However, as other people have said, it is a very bad idea to override the names of python standard library modules. You should use a different name when you import and ideally rename your module entirely to something other than math.

Importing modules dynamically in Python 3.X

I would like to import a module from inside a functions. For example from this:
from directory.folder.module import module
def import():
app.register_blueprint(module)
To this:
def import():
from directory.folder.module import module
But, without hardcoding it. For example:
def import():
m = "module"
from directory.folder.m import m
Is it possible? Thanks in advance
You want the importlib module.
Here's the most simplistic way to use this module. There are lots of different ways of weaving the results of calls to the module into the environment:
import importlib
math = importlib.import_module("math")
print(math.cos(math.pi))
Result:
-1.0
I've used this library a lot. I built a whole plug-in deployment system with it. Scripts for all the various deploys were dropped in directories and only imported when they were mentioned in a config file rather than everything having to be imported right away.
Something I find very cool about this module is what's stated at the very top of its documentation:
The purpose of the importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the import() function) in Python source code.
The intro in the 2.7 docs is interesting as well:
New in version 2.7.
This module is a minor subset of what is available in the more full-featured package of the same name from Python 3.1 that provides a complete implementation of import. What is here has been provided to help ease in transitioning from 2.7 to 3.1.
No, python import does not work this way.
Such as an example you try to import a module named mod, so you run import mod. Now interpreter will search for mod.py in a list of directories gathered from the following sources:
The directory from where the input script was run or the current directory if the interpreter is being run interactively.
The list of directories contained in the PYTHONPATH environment variable, if it is set. (The format for PYTHONPATH is OS-dependent but should mimic the PATH environment variable.)
An installation-dependent list of directories configured at the time Python is installed.
So if you have a variable named m='mod' and run import m it will search for m.py not mod.py.
But just a silly dangerous workaround is to use exec() (WARNING FOR MALICIOUS INPUT)
m = "module"
exec(f'from directory.folder.m import {m}')
If you don't mind external modules try importlib.
You can use the importlib module to programmatically import modules.
import importlib
full_name = "package." + "module"
m = importlib.import_module(full_name)

imp.load_source() in Python

When is it useful to use imp.load_source() method for importing Python module? Has it some advantage in some scenario in opposite to normal importing with import keyword?
import always looks in the following order:
already imported modules
import hooks
files in the locations in sys.path
builtin modules
If you want to import a module which would not be found by any of these mechanisms, but you know the filename, then you could use imp.load_source(). Or if you want to import a module that would be shadowed by an earlier import mechanism, for example if you want to import foo from a directory in sys.path but there is a custom import hook that would find its own version of foo first, then you could use imp.load_source() for that too. Basically it lets you control the source of the module's code in a way that import does not.

Loading native python libraries

Python 2.7 comes with json library included. In my PYTHONPATH I include third party sources and one of them is also called json. The result ending up with loaded the wrong json library. What would be a good practice to handle and avoid situations like above? Is there a way to instruct Python to explicitly load the native library in this fashion from ? import json.
You could try
from path import json as anotherjson
This way the namespace conflict can be removed.
Also you can see the discussions about relative/absolute import.
http://docs.python.org/whatsnew/2.5.html#pep-328
It says :
In Python 2.5, you can switch import‘s behaviour to absolute imports
using a from future import absolute_import directive. This
absolute- import behaviour will become the default in a future version
(probably Python 2.7). Once absolute imports are the default, import
string will always find the standard library’s version. It’s suggested
that users should begin using absolute imports as much as possible.
from __future__ import absolute_import
# from standard path
import json as _json
# from a package
from pkg import json as pkgjson
The other technique is to use the imp module
import imp
json = imp.load_source('json', '/path/to/json.py')
There really is no good way to have multiple modules with the same name on PYTHONPATH[docs], this means that you should probably move the third party json module to an alternate location that is not on PYTHONPATH, and then import it using some other method.
The easiest way to do this is to move the third party json module into a subdirectory of the location it is already in, and then make that subdirectory a module by adding __init__.py to it.
If you named this new directory 'thirdparty', you could then import your third party json module using from thirdparty import json, and import json would always import Python's json module.
Alternatively, you could rename the module to something that does not conflict.

Using imported modules in more than one file

This question is a bit dumb but I have to know it. Is there any way to use imported modules inside other imported modules?
I mean, if I do this:
-main file-
import os
import othermodule
othermodule.a()
-othermodule-
def a():
return os.path.join('/', 'example') # Without reimporting the os module
The os module is not recognized by the file. Is there any way to "reuse" the os module?
There's no need to do that, Python only loads modules once (unless you unload them).
But if you really have a situation in which a module can't access the standard library (care to explain???), you can simply access the os module within the main module (e.g. mainfile.os, modules are just variables when imported into a module namespace).
If the os module is already loaded, you can also access it with sys.modules["os"].
You have to put import os in othermodule.py as well (or instead, if "main file" doesn't need os itself). This is a feature; it means othermodule doesn't have to care what junk is in "main file". Python will not read the files for os twice, so don't worry about that.
If you need to get at the variables in the main file for some reason, you can do that with import __main__, but it's considered a thing to be avoided.
If you need a module to be reread after it's already been imported, you probably should be using execfile rather than import.
Python only imports a module once. Any subsequent import calls, just access the existing module object.

Categories

Resources