Python module name number

In python, how to import filename starts with a number

If you can’t rename the module to match Python naming conventions, create a new module to act as an intermediary: Solution: First, note that PyPI project names and module names are completely independent; there’s nothing stopping you from creating a package that installs a module , and these two names follow separate policies as to what is valid. Module names are restricted by Python’s grammar to be valid identifiers.

In python, how to import filename starts with a number

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.

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.

Python – Import module from different directory, We can use sys.path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that

Python Tutorial — Import module from different directory

In this video, let’s talk about import statement in python and why do we need import modules Duration: 18:41

How to import module when module name has a ‘-‘ dash or hyphen in it?

Starting from Python 3.1, you can use importlib :

import importlib foobar = importlib.import_module("foo-bar") 

you can’t. foo-bar is not an identifier. rename the file to foo_bar.py

Edit: If import is not your goal (as in: you don’t care what happens with sys.modules , you don’t need it to import itself), just getting all of the file’s globals into your own scope, you can use execfile

# contents of foo-bar.py baz = 'quux' 
>>> execfile('foo-bar.py') >>> baz 'quux' >>> 

If you can’t rename the module to match Python naming conventions, create a new module to act as an intermediary:

 ---- foo_proxy.py ---- tmp = __import__('foo-bar') globals().update(vars(tmp)) ---- main.py ---- from foo_proxy import * 

Python Modules: Learn to Create and Import Custom, Python fromimport statement We can import specific names from a module without importing the module as a whole. Here is an example. Here, we imported

Is it acceptable to have python package names with numbers in it?

First, note that PyPI project names and module names are completely independent; there’s nothing stopping you from creating a package foo that installs a module bar , and these two names follow separate policies as to what is valid.

Module names are restricted by Python’s grammar to be valid identifiers. In Python 2, this means that they must consist of an ASCII letter or underscore followed by zero or more ASCII letters, digits, and/or underscores. In Python 3, Unicode is added, and things get more complicated, but I believe that all-ASCII module names still follow the same restrictions.

The names of projects on PyPI (as specified in PEP 508, among others) must consist entirely of ASCII letters, numbers, . , — , and/or _ , and they must begin & end with a letter or number. There is also a normalization policy that enforces case-insensitivity and treats runs of . , — , and _ as equal, so foo-bar and FOO.BAR are considered the same project.

In addition, PEP 8 has a section on package and module names; it says:

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket ).

So, yes, you can have a number in both a project name and a module name, and the project name can even begin with one!

Python3 import file from directory starting with number, Assuming your script is in the aale directory, you will need to do your import like: two = importlib.import_module(‘aale.2.2’).

Import module with name same as built-in module in python 3

It’s pretty bad practice to name your modules after built-in modules. I’d recommend naming your math.py something else.

That being said, you could import it using the path with imp:

import imp math = imp.load_source('math', './math.py') 

Dove into a bit of a rabbit hole but here we go. As a disclaimer, like Jack said naming modules after builtins is very bad practice, and this can more easily be accomplished using imp as he suggested.

The reason you’re having problems come from the interaction of a few things. When you type

What your python does is look at sys.path. It will check in all the locations in sys.path for a module named math, and import the first one it finds. In your case, it finds your local math module first. After the import is completed, it adds it to sys.modules, but we’ll get back to that.

Since you want to use both, first you can import your local math as you have. I would suggest importing it as a different name to keep it separate.

from . import math as local_math 

After that, we need to mess with our sys.path to find the builtin math

This reverses the order of sys.path, meaning it will look in your local directory first instead of last.

Now you might think this was enough, but if you try to import math here, python will see it in sys.modules and not bother trying to import again, so first

Then we can import the default math module

and finally clean up our sys.path

now we have access to everything we need

>>>math.cos(10) -0.8390715290764524 >>>local_math.f(10) 1000 

Importing a class from a module starting with number, importlib.import_module(«123_uni_class») returns the module after importing it, you must give it a valid name in order to reuse it:

Источник

Читайте также:  Adding css to django template
Оцените статью