How to reference python package when filename contains a period

PythonImportModulePackagePython Import

Python Problem Overview


I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:

from "models.admin" import *

however, I get a syntax error for having double quotes. But if I just do

from models.admin import *

then I get "ImportError: No module named admin"

Is there any way to import from a python file that has a period in its name?

Python Solutions


Solution 1 - Python

Actually, you can import a module with an invalid name. But you'll need to use imp for that, e.g. assuming file is named models.admin.py, you could do

import imp
with open('models.admin.py', 'rb') as fp:
    models_admin = imp.load_module(
        'models_admin', fp, 'models.admin.py',
        ('.py', 'rb', imp.PY_SOURCE)
    )

But read the docs on imp.find_module and imp.load_module before you start using it.

Solution 2 - Python

If you really want to, you can import a module with an unusual filename (e.g., a filename containing a '.' before the '.py') using the imp module:

>>> import imp
>>> a_b = imp.load_source('a.b', 'a.b.py')
>>> a_b.x
"I was defined in a.b.py!"

However, that's generally a bad idea. It's more likely that you're trying to use packages, in which case you should create a directory named "a", containing a file named "b.py"; and then "import a.b" will load a/b.py.

Solution 3 - Python

The file is called models/admin.py. (Source)

That is, it should be called admin.py in a directory called models.

Then you can import using from models.admin import *, assuming that it is in your Python path.

Solution 4 - Python

No, you can't import a python file as a module if its name contains a period (or a question mark, or exclamation mark, etc). A python module's name (not including the .py) must be a valid python name (ie can be used as a variable name).

Solution 5 - Python

Like below

Assume dir structure is like this:

C:.
│   script.py
│   
└───Parent
    └───Child
        ├───1.1
        │       main.py
        │       
        └───1.2

**assume you want to import main.py in script.py **

your main.py looks like below

def my_function():
  print("Hello from a function")

your script.py looks like below

from os import path
import importlib
from os.path import dirname
import sys
import importlib.util


def getPath():
    # your logic to get to the path
    return path.join(dirname(__file__),'Parent','Child','1.1','main.py')

file_path = getPath() 
module_name = 'main'

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

#call functions like this
module.my_function()

Check out this gist

Solution 6 - Python

You are not referencing files in the import statement, you are referencing modules and packages.

Please read the docs, they are very clear on that matter.

Anyway, since you are using django, the usual approach won't work. If you want to keep models in separate files, rather than in models.py, you have to take extra steps, outlined, for example, here.

Edit:
Well, I don't really know what the questioneer means when he mentions admin and whether or not it is related to the admin interface of django. My points still stand.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander BirdView Question on Stackoverflow
Solution 1 - PythonCat Plus PlusView Answer on Stackoverflow
Solution 2 - PythonEdward LoperView Answer on Stackoverflow
Solution 3 - PythonMark ByersView Answer on Stackoverflow
Solution 4 - PythonGabriel ReidView Answer on Stackoverflow
Solution 5 - PythonSciTech EnthusiastView Answer on Stackoverflow
Solution 6 - PythonshylentView Answer on Stackoverflow