Calling a standard library from imported module - python

Is it a good idea to use a standard library function from an imported module? For example, I write a xyz.py module and within xyz.py, I've this statetment import json
I have another script where I import xyz. In this script I need to make use of json functions. I can for sure import json in my script but json lib was already imported when I import xyz. So can I use xyz.json() or is it a bad practice?

You should use import json again to explicitly declare the dependency.
Python will optimize the way it loads the modules so you don't have to be concerned with inefficiency.
If you later don't need xyz.py anymore and you drop that import, then you still want import json to be there without having to re-analyze your dependencies.

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.

How can I hook a function in a python module?

So I have a package in my virtual environment installed in the site-packages folder of it.
In that package there is a module with a bunch of functions, I want to change one of these functions without touching the package code, so I want to make a hook of that module and then change/overwrite the function so it behaves in the way I want.
I don't know if this is even posible in python (I'm new to the language)
Example:
I have a package called their_package in my site-packages folder, inside I have a module named whatever.py with the following code:
import sys
import os
def this_is_a_function(parameter):
//whatever logic in here
return result
What I want is to hook the whatever.py module so I can redefine the this_is_a_function to have a diferent logic. Is this posible? I would appreciate a lot a code sample!
Thank you in advance!:)
You can redefine your function with:
import whatever
def this_is_a_function(parameter):
pass
whatever.this_is_a_function = this_is_a_function

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