In python, how to import filename starts with a number - python

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.

Related

Can you perform an inline import in 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).

How to find if given python import statement is internal or external?

I have an excel sheet having thousands of import statements.
eg.,
from XYZ.loghelper import LogHelper,
import os,
from models import CustomUser, VerticalApp,
from django.http import HttpResponse,
Some of them are built-in and some are user defined.
Now I have to find whether they are user defined or built-in.
How can I do that?
I assume that by builtin you mean "part of Python's stdlib" (python's "builtin" features, being "builtin", don't need to be imported at all). The definition of "user defined" is much more vague - is a 3rd part package like Django "builtin" or "user-defined" ?
But anyway: the short answer is that technically you CAN NOT tell just from the import statement. Modules are looked up in sys.path and the first matching module will be selected, so if you have a module named "os.py" in a local directory that comes before your Python installation's stdlib directory in sys.path then "import os" will indeed import your own "os.py" module instead of the stdlib's one. IOW, you need to use the exact same environment, import the module, and check the module's __file__ attribute to find out where it's been imported from.
Now most python devs try to avoid shadowing stdlib's module names (for obvious reasons) so you can also just build a list (technically you want a set for better perfs but anyway) of Python's stdlibs modules names, parse your imports statements, and check if the name of the module to be imported belongs to the set of stdlib's names. This should yield correct results in most cases, but it's not garanteed to be 100% accurate.

Importing modules in python (3 modules)

Let's say i have 3 modules within the same directory. (module1,module2,module3)
Suppose the 2nd module imports the 3rd module then if i import module2 in module 1. Does that automatically import module 3 to module 1 ?
Thanks
No. The imports only work inside a module. You can verify that by creating a test.
Saying,
# module1
import module2
# module2
import module3
# in module1
module3.foo() # oops
This is reasonable because you can think in reverse: if imports cause a chain of importing, it'll be hard to decide which function is from which module, thus causing complex naming conflicts.
No, it will not be imported unless you explicitly specify python to, like so:
from module2 import *
What importing does conceptually is outlined below.
import some_module
The statement above is equivalent to:
module_variable = import_module("some_module")
All we have done so far is bind some object to a variable name.
When it comes to the implementation of import_module it is also not that hard to grasp.
def import_module(module_name):
if module_name in sys.modules:
module = sys.modules[module_name]
else:
filename = find_file_for_module(module_name)
python_code = open(filename).read()
module = create_module_from_code(python_code)
sys.modules[module_name] = module
return module
First, we check if the module has been imported before. If it was, then it will be available in the global list of all modules (sys.modules), and so will simply be reused. In the case that the module is not available, we create it from the code. Once the function returns, the module will be assigned to the variable name that you have chosen. As you can see the process is not inefficient or wasteful. All you are doing is creating an alias for your module. In most cases, transparency is prefered, hence having a quick look at the top of the file can tell you what resources are available to you. Otherwise, you might end up in a situation where you are wondering where is a given resource coming from. So, that is why you do not get modules inherently "imported".
Resource:
Python doc on importing

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

Categories

Resources