Reloading modules with importlib in IPython - python

I'm confused by the behaviour of importlib in the interactive IPython shell. When I run:
import importlib.reload as ilrel
I get the error ImportError: No module named 'importlib.reload'. This is exactly how I import other modules, such as matplotlib.pyplot.
Currently I've been simply importing importlib alone, then using importlib.reload(<module name>). This isn't a major issue but why does the first method not work?

Because reload is not a module, it's a function within the importlib module. There is nothing specific about importlib; this is how imports work for all modules.
If you just want the function itself, you can do from importlib import reload.
Note also that this function is only available in Python 3.4+.

Related

How to find everything imported by an import statement from python documentation?

I was trying to some code in python and noted this peculiar case.
import importlib
print(importlib.abc)
The above code runs fine in python 3.7 but not in python 3.10.2. In python 3.10.2, i get the following error:
AttributeError: module 'importlib' has no attribute 'abc'. Did you mean: '_abc'?
I tried to look at the documentation as to whether the behaviour of the above code got changed somehow. But couldn't find it. The documentations i tried looking are Python 3.7 importlib documentation and Python 3.10.2 importlib documentation.
So i wish to know how to find things imported by an import statement on a python standard library through documentation.
Note: I do know that i can look at dir(importlib) to see everything importlib has after it is imported. But i want to find it through documentation so i can know them without using that specific python version.
If you want to import a submodule of a package, you should do so explicitly:
import importlib.abc
Python will not automatically load all submodules of a package when you import the package. Whether submodules will be available without an explicit submodule import depends on whether some code somewhere else in the program has already loaded the module.
As for the question in the title, documentation will usually not have the information you're asking for. Good documentation will thoroughly describe a module's entire public API, but you're asking for information beyond that.
You could use python dir() function to know what importlib contains
import importlib
dir(importlib)
Or a slightly different method is hasattr(importlib,'abc') which returns a boolean.
Yet again another way is via the inspect module
import importlib
import inspect
inspect.getmembers(importlib) #Show all methods,attribute and ...
inspect.getmembers(importlib, inspect.ismodule) #Show only modules

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)

How to use globally imported packages in script

I want to put my interactive commands in a script, but I can't run the same commands in the script.
We are using a heavily packaged version of Python for our tests, we usually run tests in interactive mode, but now I want to place all the commands in a package. Below is an example using the time package.
In interactive mode:
>>> import time
>>> import myscript
In my script:
time.sleep(5)
I expected the script to refer to the globally imported packages and allow me to run sleep, but it says NameError: global name 'time' is not defined
How do I get my script to recognize all packages imported into the interactive terminal? We use thousands of packages in our toolkit, and I can't import them all into my script.
You have to import these libraries also in the .py file where you are going to use them. Python does not allow using them when they are imported in a higher level module, and that's the way it should be. Python, in some way, forces you being better programmer. Your script should be something like this:
import time
time.sleep(5)
If I have Module A:
import time
and Module B:
import A
then in module B I do have access to libraries that A imported, but they must be qualified thus:
A.time.sleep5()
In short, when you import a module, the public names in that module become accessible to the importer. But what you are attempting to do is quite different. In essence, you have Module A as:
import time
import B
And module B as:
time.sleep(5)
Module B neither directly imports the time package nor module A and therefore has no access to time. Being imported by a module that does have access to time does not confer to the imported module that access.

ImportError: No module named primes

I am trying to use python and cython under Linux environment, so I am trying to use the primes.pyx example mentioned in the cython documentation.
I have created primes.pyx in xyz directory and I have compiled that file using cython primes.pyx. After invoking the python interpreter and then trying import primes I ran into the following issue.
ImportError: No module named primes
Could some body help me.
Cython modules need to be compiled before they can be used. You have two (good) options: either make a setup.py for the module, as described at this page, or use pyximport to automatically compile and build your module with just an import.
The first option is best if you have a module with other library dependencies, or spread across multiple files. The second option is best for small, self-contained modules.
To use pyximport, add import pyximport; pyximport.install() before importing any Cython modules. Then, when you import a Cython module, Cython will compile the module automatically if needed.
This sounds to me like it could be a case of having forgotten to import the module that lets you import straight .pyx files without a setup.py. Documentation on that can be found here.
Try adding
import pyximport; pyximport.install()
to the top of your module, or running it in the interpreter before trying to import primes.

Categories

Resources