Import a Python Module Without the .Py Extension

Import a python module without the .py extension

You can use the imp.load_source function (from the imp module), to load a module dynamically from a given file-system path.

import imp
foobar = imp.load_source('foobar', '/path/to/foobar')

This SO discussion also shows some interesting options.

How to import a python script without a .py extension?

You can do it like this:

from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader

spec = spec_from_loader("test", SourceFileLoader("test", "/path/to/test.cookie"))
test = module_from_spec(spec)
spec.loader.exec_module(test)

Import a python module without .py extension,

You can use imp.load_source

>>> import imp
>>> mod = imp.load_source("test", "test")
hello world
>>> mod.a
1

abc:

print "hello world"
a = 1

Python imports: importing a module without .py extension?

You could most likely use some tricker by using import hooks, I wouldn't recommend it though. On the other hand I would also probably do it the other way around , have your .py scripts somewhere, and make '.py'less symbolic links to the .py files. So your library could be anywhere and you can run the test from within by importing it normall (since it has the py extension), and then /usr/bin/yetanotherfeature points to it, so you can run it without the py.

Edit: Nevermind this (at least the hooks part), the import imp solution looks very good to me :)

Import python from different folder without using .py extension

It is not possible to do this using import directly. It's best to simply rename the file to .py

That being said, it's possible to load the module into a variable using importlib or imp depending on your Python version.


Given the following file at path ./distribute (relative to where python is run):

# distribute

print("Imported!")
a_var = 5

Python 2

# python2import

from imp import load_source

distribute = load_source("distribute", "./distribute")
print(distribute.a_var)

Usage:

$ python python2import
Imported!
5

Python 3

#python3import

from importlib.machinery import SourceFileLoader

distribute = SourceFileLoader("distribute", "./distribute").load_module()
print(distribute.a_var)

Usage:

$ python3 python3import
Imported!
5

Importing file (without extension) contents into python script as object

Do not complicate yourself using import passc or from passc import * of this way only we can import files with .py extension.

The fast solution to fix this problem is doing something like this:

# read file
my_file=open("passc","r")
# save code in a var
code = my_file.read()

print(code)
# aABBccDd01234

Cheers!

How do I import other Python files?

importlib was added to Python 3 to programmatically import a module.

import importlib

moduleName = input('Enter module name:')
importlib.import_module(moduleName)

The .py extension should be removed from moduleName. The function also defines a package argument for relative imports.

In python 2.x:

  • Just import file without the .py extension
  • A folder can be marked as a package, by adding an empty __init__.py file
  • You can use the __import__ function, which takes the module name (without extension) as a string extension
pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))

Type help(__import__) for more details.

How can I import a module dynamically given the full path?

For Python 3.5+ use (docs):

import importlib.util
import sys
spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
foo = importlib.util.module_from_spec(spec)
sys.modules["module.name"] = foo
spec.loader.exec_module(foo)
foo.MyClass()

For Python 3.3 and 3.4 use:

from importlib.machinery import SourceFileLoader

foo = SourceFileLoader("module.name", "/path/to/file.py").load_module()
foo.MyClass()

(Although this has been deprecated in Python 3.4.)

For Python 2 use:

import imp

foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()

There are equivalent convenience functions for compiled Python files and DLLs.

See also http://bugs.python.org/issue21436.

module as script without .py in Python?

In general, the solution here is to leave foobar.py as a module, and have the script be something as simple as:

import foobar
foobar.main()

If you're installing your module using setup.py (and you probably should be), you can also do this via console_scripts entry points, like this:

entry_points={
'console_scripts': [
'foobar = foobar:main'
],
},

This will install a foobar command that is roughly equivalent to the stub in the first part of this answer.



Related Topics



Leave a reply



Submit