Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module name different than directory name?

Tags:

Let's assume I have a python package called bestpackage.

Convention dictates that bestpacakge would also be a directory on sys.path that contains an __init__.py to make the interpreter assume it can be imported from.

Is there any way I can set a variable for the package name so the directory could be named something different than the directive I import it with? Is there any way to make the namespacing not care about the directory name and honor some other config instead?

My super trendy client-side devs are just so much in love with these sexy something.otherthing.js project names for one of our smaller side projects!

EDIT:

To clarify, the main purpose of my question was to allow my client side guys continue to call the directories in their "projects" (the one we all have added to our paths) folder using their existing convention (some.app.js), even though in some cases they are in fact python packages that will be on the path and sourced to import statements internally. I realize this is in practice a pretty horrible thing and so I ask more out of curiosity. So part of the big problem here is circumventing the fact that the . in the directory name (and thereby the assumed package name) implies attribute access. It doesn't really surprise me that this cannot be worked around, I was just curious if it was possible deeper in the "magic" behind import.

There's some great responses here, but all rely on doing a classical import of some kind where the attribute accessor . will clash with the directory names.

like image 550
DeaconDesperado Avatar asked Nov 06 '12 15:11

DeaconDesperado


People also ask

How can I import modules if file is not in same 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 can also look for the module in that directory if it doesn't find the module in its current directory. As sys.

How do I change a Python module name?

If you really want to import the file 'oldname.py' with the statement 'import newname', there is a trick that makes it possible: Import the module somewhere with the old name, then inject it into sys. modules with the new name. Subsequent import statements will also find it under the new name.

Is a Python module a folder?

You need to have a good understanding of Python modules and packages to know how imports work. A Python module is a file that has a . py extension, and a Python package is any folder that has modules inside it (or, in Python 2, a folder that contains an __init__.py file).

What should I name my Python modules?

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.


1 Answers

A directory with a __init__.py file is called a package.

And no, the package name is always the same as the directory. That's how Python can discover packages, it matches it against directory names found on the search path, and if there is a __init__.py file in that directory it has found a match and imports the __init__.py file contained.

You can always import something into your local module namespace under a shorter, easier to use name using the from module import something or the import module as alias syntax:

from something.otherthing.js import foo from something.otherthing import js as bar import something.otherthing.js as hamspam 
like image 139
Martijn Pieters Avatar answered Sep 16 '22 14:09

Martijn Pieters