Can you perform an inline import in Python? - python

Say you only wanted to call a regular expression a single time in you code. As far as I am aware, this means that you then need to do import re somewhere before your call of a function from re. Is it possible to combine this with the function call, in-line?
I thought maybe something like this would work
print(import re; re.search(r'<regex>', <string>).group())
but it just threw an error saying invalid syntax at the point of the import. This leads me to believe that the only way to do this is
import re
print(re.search(r'<regex>'), <string>).group())

Answering the question:
Can You Perform an Inline Import in Python?
You can use the built-in importlib module:
print(importlib.import_module('re').search("h", "hello").group())
Output:
'h'
Of course, it would require you to import the importlib module first:
import importlib
print(importlib.import_module('re').search("h", "hello").group())
From the documentation:
The import_module() function acts as a simplifying wrapper around importlib.__import__(). This means all semantics of the function are derived from importlib.__import__(). The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg.mod), while __import__() returns the top-level package or module (e.g. pkg).

Related

how to import list from other module in python if module name starts with a number [duplicate]

Basically there is a file called 8puzzle.py and I want to import the file into another file (in the same folder and I cannot change the file name as the file is provided). Is there anyway to do this in Python? I tried usual way from 8puzzle import *, it gives me an error.
Error is:
>>> import 8puzzle
File "<input>", line 1
import 8puzzle
^
SyntaxError: invalid syntax
>>>
You could do
puzzle = __import__('8puzzle')
Very interesting problem. I'll remember not to name anything with a number.
If you'd like to import * -- you should check out this question and answer.
The above answers are correct, but as for now, the recommended way is to use import_module function:
importlib.import_module(name, package=None)
Import a module. The name
argument specifies what module to import in absolute or relative terms
(e.g. either pkg.mod or ..mod). If the name is specified in relative
terms, then the package argument must be set to the name of the
package which is to act as the anchor for resolving the package name
(e.g. import_module('..mod', 'pkg.subpkg') will import pkg.mod).
The import_module() function acts as a simplifying wrapper around
importlib.__import__(). This means all semantics of the function are
derived from importlib.__import__(). The most important difference
between these two functions is that import_module() returns the
specified package or module (e.g. pkg.mod), while __import__() returns
the top-level package or module (e.g. pkg).
If you are dynamically importing a module that was created since the
interpreter began execution (e.g., created a Python source file), you
may need to call invalidate_caches() in order for the new module to be
noticed by the import system.
__import__ is not recommended now.
importlib.__import__(name, globals=None, locals=None, fromlist=(), level=0)
An implementation of the built-in __import__() function.
Note Programmatic importing of modules should use import_module() instead of this function.
The file directory structure is as follows:
daily
-- 20210504
permutations.py
__init__.py
__init__.py
You can import the permutations module by __import__ or importlib.import_module.
The official documentation recommends using importlib.import_module.
import(name, globals=None, locals=None, fromlist=(), level=0) -> module
Import a module. Because this function is meant for use by the Python interpreter and not for general use, it is better to
useimportlib.import_module()to programmatically import a module.
What is the difference?
If implemented using __import__.
For example:
res = __import__('daily.20210504.permutations')
The result of res is the daily module.
So, if you want to get the permutations module, you need to provide the fromlist parameter, which is written as follows.
res = __import__('daily.20210504.permutations', fromlist=('daily.20210504'))
The result of res can be seen now as
That's the right result.
What if I use importlib.import_module?
res = importlib.import_module('daily.20210504.permutations')
this allows you to get the permutations module directly.
Don't use the .py extension in your imports.
Does from 8puzzle import * work?
For what it's worth, from x import * is not a preferred Python pattern, as it bleeds that module's namespace into your current context.
In general, try to import things you specifically want from that module. Any global from the other module can be imported.
e.g., if you have 8puzzle.foo you could do `from 8puzzle import
Edit:
While my .py message is correct, it isn't sufficient.
The other poster's __import__('8puzzle') suggestion is correct. However, I highly recommend avoiding this pattern.
For one, it's reserved an internal, private Python method. You are basically breaking the fundamental assumptions of what it means to be able to import a module. Simply renaming the file to something else, like puzzle8, will remedy this.
This will frustrate the hell out of experienced Python programmers who are expecting to know what your imports are at the top and are expecting code to (try to) conform to PEP8.

import vs __import__( ) vs importlib.import_module( )?

I noticed Flask was using Werkzeug to __import__ a module, and I was a little confused. I went and checked out the docs on it and saw that it seems to give you more control somehow in terms of where it looks for the module, but I'm not sure exactly how and I have zero idea how it's different from importlib.import_module.
The odd thing in the Werkzeug example is that it just says __import__(import_name), so I don't see how that's any different from just using the import statement, since it's ignoring the optional extra parameters.
Can anyone explain? I looked at other people having asked similar questions on SO previously but they weren't very clearly phrased questions and the answers didn't address this at all.
__import__ is a low-level hook function that's used to import modules; it can be used to import a module dynamically by giving the module name to import as a variable, something the import statement won't let you do.
importlib.import_module() is a wrapper around that hook* to produce a nice API for the functionality; it is a very recent addition to Python 2, and has been more fleshed out in Python 3. Codebases that use __import__ generally do so because they want to remain compatible with older Python 2 releases, e.g. anything before Python 2.7.
One side-effect of using __import__ can be that it returns the imported module and doesn't add anything to the namespace; you can import with it without having then to delete the new name if you didn't want that new name; using import somename will add somename to your namespace, but __import__('somename') instead returns the imported module, which you can then ignore. Werkzeug uses the hook for that reason in one location.
All other uses are to do with dynamic imports. Werkzeug supports Python 2.6 still so cannot use importlib.
* importlib is a Pure-Python implementation, and import_module() will use that implementation, whist __import__ will use a C-optimised version. Both versions call back to importlib._bootstrap._find_and_load() so the difference is mostly academic.
__import__(import_name), so I don't see how that's any different from
just using the import statement
Both __import__() and importlib.import_module() allow you to import a module when you have the module name as a string. You cannot write:
x = 're'
import x
or you'll get:
File "1.py", line 3, in <module>
import x ImportError: No module named x

Import Numerous Modules

I'm working on my making a simple tester for my project, my project is a group of many self contained python files (e.g. solution1.py, solution2.py, etc.). My tester works, but it requires me to do import solution1, solution2, ..., how can I just import everything that matches a pattern?
I have tried creating a list of the files using glob and importing the list, but that gave:
ImportError: No module named solutions
I tried to eval() it, but that was 'invalid syntax'.
Perhaps the question is, how can I interpolate a list of strings into a list of modules?
Thanks!
You can import a module identified by a string with the importlib module:
import importlib
list_of_module_names = ...
for s in list_of_module_names:
importlib.import_module(s)
You can use importlib.import_module() to import a module named by a string.
If you wanted to construct an import statement instead, you'd need to use exec() instead of eval(). The latter can only evaluate expressions; the former executes statements.

Python: uniquely identify a function from a module

I am not really a programmer but a computational statistician, so I may understand complex algorithms but not simple programming constructs.
My original problem is to check within a function if a module function is callable. I looked around and decided to go for a try (call function) - except (import module) to make it simple. I'd love to search sys.mod for this but I am running in some identifiability problems.
My current problem is that there are many ways of importing a function from a module: import module will define the function as module.function but from module import function will define it as function. Not to mention from module import function as myfunction. Therefore the same function can be called in several different ways.
My question is: is there a unique "signature" for a function that can be traced if the module is loaded? It would be fantastic to have the actual call alias to it.
ps besides: mod is mathematical function and sys.mod returns a list of loaded modules, but python (2.7) does not complain when you shadow the built-in mod function by doing the following, from sys import mod. I find this a bit awkward - is there any way to avoid this sort of shadowing programatically?
My original problem is to check within a function if a module function is callable.
By definition, all functions are callable. This will test if an object is callable: http://docs.python.org/library/functions.html#callable
Therefore the same function can be called in several different ways.
Yes, but it will be the same object. You can just use f is g to test if f and g are the same object.
Update: Why would you need to use a unique ID? Seriously, don't do this. You have is for identity tests, and the __hash__ method to define the hash function applicable.
It would be fantastic to have the actual call alias to it.
Not sure at all what you mean, but I think you just want it to always be one object. Which it is already.
mod is mathematical function and sys.mod returns a list of loaded modules, but python (2.7) does not complain to from sys import mod. I find this a bit awkward?
Then don't do that. You know about the import ... as syntax. Also mod is not by default in the global namespace (the operator % is for that).
Finally, python does complain about your import line:
>>> from sys import mod
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name mod
(Thanks to kindall for pointing this out).
Assume I have a module with the following:
def foo(): pass
bar = foo
You can easily see that they're the same functions by using is or id():
>>> import demo
>>> from demo import *
>>> demo.foo is foo
True
>>> id(demo.foo) == id(foo)
True
>>> demo.bar is foo
True
They all refer to the same code object, it's just stored under different names in the scope dictionary.
# define modulus f
def mod(a,b):
return b % a
print mod(5,2)
alias:
modulus=mod
print modulus(5,2)
this is pretty pythonic construct, and it is pretty intuitive for mathematicians
different ways of import serve to help you place a function into different "name space" for later use in your program, sometimes you wish to use a function a lot so you choose variant that is shorter to write.
you can also do something like:
myat=math.atanh
to make alias in another "name space"
and use it as:
myat(x)
as it would use math.atanh(x) - becomes shorter to write
Typical programmers approach would be define all you want to use and then use it. What you are trying in my belief is to do it "lazy" => import module when you need a function. That is why you wish to know if function is "callable".
Python is not functional programming language (e.g. like haskel) so that you can load or refer "on demand".
hope this helps.

In python, how to import filename starts with a number

Basically there is a file called 8puzzle.py and I want to import the file into another file (in the same folder and I cannot change the file name as the file is provided). Is there anyway to do this in Python? I tried usual way from 8puzzle import *, it gives me an error.
Error is:
>>> import 8puzzle
File "<input>", line 1
import 8puzzle
^
SyntaxError: invalid syntax
>>>
You could do
puzzle = __import__('8puzzle')
Very interesting problem. I'll remember not to name anything with a number.
If you'd like to import * -- you should check out this question and answer.
The above answers are correct, but as for now, the recommended way is to use import_module function:
importlib.import_module(name, package=None)
Import a module. The name
argument specifies what module to import in absolute or relative terms
(e.g. either pkg.mod or ..mod). If the name is specified in relative
terms, then the package argument must be set to the name of the
package which is to act as the anchor for resolving the package name
(e.g. import_module('..mod', 'pkg.subpkg') will import pkg.mod).
The import_module() function acts as a simplifying wrapper around
importlib.__import__(). This means all semantics of the function are
derived from importlib.__import__(). The most important difference
between these two functions is that import_module() returns the
specified package or module (e.g. pkg.mod), while __import__() returns
the top-level package or module (e.g. pkg).
If you are dynamically importing a module that was created since the
interpreter began execution (e.g., created a Python source file), you
may need to call invalidate_caches() in order for the new module to be
noticed by the import system.
__import__ is not recommended now.
importlib.__import__(name, globals=None, locals=None, fromlist=(), level=0)
An implementation of the built-in __import__() function.
Note Programmatic importing of modules should use import_module() instead of this function.
The file directory structure is as follows:
daily
-- 20210504
permutations.py
__init__.py
__init__.py
You can import the permutations module by __import__ or importlib.import_module.
The official documentation recommends using importlib.import_module.
import(name, globals=None, locals=None, fromlist=(), level=0) -> module
Import a module. Because this function is meant for use by the Python interpreter and not for general use, it is better to
useimportlib.import_module()to programmatically import a module.
What is the difference?
If implemented using __import__.
For example:
res = __import__('daily.20210504.permutations')
The result of res is the daily module.
So, if you want to get the permutations module, you need to provide the fromlist parameter, which is written as follows.
res = __import__('daily.20210504.permutations', fromlist=('daily.20210504'))
The result of res can be seen now as
That's the right result.
What if I use importlib.import_module?
res = importlib.import_module('daily.20210504.permutations')
this allows you to get the permutations module directly.
Don't use the .py extension in your imports.
Does from 8puzzle import * work?
For what it's worth, from x import * is not a preferred Python pattern, as it bleeds that module's namespace into your current context.
In general, try to import things you specifically want from that module. Any global from the other module can be imported.
e.g., if you have 8puzzle.foo you could do `from 8puzzle import
Edit:
While my .py message is correct, it isn't sufficient.
The other poster's __import__('8puzzle') suggestion is correct. However, I highly recommend avoiding this pattern.
For one, it's reserved an internal, private Python method. You are basically breaking the fundamental assumptions of what it means to be able to import a module. Simply renaming the file to something else, like puzzle8, will remedy this.
This will frustrate the hell out of experienced Python programmers who are expecting to know what your imports are at the top and are expecting code to (try to) conform to PEP8.

Categories

Resources