I have a file called foobar (without .py extension). In the same directory I have another python file that tries to import it:

import foobar

But this only works if I rename the file to foobar.py. Is it possible to import a python module that doesn't have the .py extension?

Update: the file has no extension because I also use it as a standalone script, and I don't want to type the .py extension to run it.

Update2: I will go for the symlink solution mentioned below.

share|improve this question
1  
I'm intrigued. Why do you have a python file without the py extension? – voyager Apr 8 '10 at 16:52
1  
Sometimes it's nice to use python for configuration files (extension as .conf) or to denote a special type of file. In my case, it'd be more of a convenience for an Administrator. – NuclearPeon Aug 2 '13 at 3:01
1  
I have a file with configuration that is used both as a python file and as a bash script. I gave it a pysh extension... – osa Jul 11 '14 at 21:43
    
If that is configuration related things, I recommend using ConfigParser. wiki.python.org/moin/ConfigParserExamples – Chemical Programmer Nov 23 '15 at 7:53
1  
@voyager One reason is python scripts with .cgi extensions instead of .py extension – repzero Jun 15 '16 at 1:49
up vote 31 down vote accepted

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

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

This SO discussion also shows some interesting options.

share|improve this answer
    
Fixed. [it is more constructive to suggest an Edit, though] – Eli Bendersky May 14 '15 at 14:02
    
but again section is required as I understood from >>> foobar = imp.load_source('','credentials') [default] NameError: name 'default' is not defined – Ilja Oct 11 '15 at 10:16
2  
What is the use of the first argument 'foobar' if it is assigned in the return value? – Anmol Singh Jaggi Apr 30 '16 at 14:13
2  
@AnmolSinghJaggi It sets the module __name__ property; normally that's determined from the filename, but since you're using a non-standard filename (which might not even contain any valid python identifier at all), you have to specify the module name. The variable name in which you store a reference to the created module object is irrelevant, much as if you import foo.bar as baz the module referenced by the variable baz will still have its original __name__. – Ben Apr 26 '17 at 2:40
    
This has been deprecated since 3.4. Any idea how to import from a file without the .py extension in 3.4+? – exhuma Oct 6 '17 at 9:42

Here is a solution for Python 3.4+:

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

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

Using spec_from_loader and explicitly specifying a SourceFileLoader will force the machinery to load the file as source, without trying to figure out the type of the file from the extension. This means that you can load the file even though it is not listed in importlib.machinery.SOURCE_SUFFIXES.

If you want to keep importing the file by name after the first load, add the module to sys.modules:

sys.modules['foobar'] = foobar
share|improve this answer

If you install the script with package manager (deb or alike) another option would be to use setuptools:

"...there’s no easy way to have a script’s filename match local conventions on both Windows and POSIX platforms. For another, you often have to create a separate file just for the “main” script, when your actual “main” is a function in a module somewhere... setuptools fixes all of these problems by automatically generating scripts for you with the correct extension, and on Windows it will even create an .exe file..."

https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation

share|improve this answer

Like others have mentioned, you could use imp.load_source, but it will make your code more difficult to read. I would really only recommend it if you need to import modules whose names or paths aren't known until run-time.

What is your reason for not wanting to use the .py extension? The most common case for not wanting to use the .py extension, is because the python script is also run as an executable, but you still want other modules to be able to import it. If this is the case, it might be beneficial to move functionality into a .py file with a similar name, and then use foobar as a wrapper.

share|improve this answer
12  
Or instead of wrapping, just symlink foobar.py to foobar (assuming you aren't on Windows) – whaley Apr 8 '10 at 18:31
    
@whaley, yeah, that would be much cleaner. You could use a .bat for windows to accomplish the same thing. – user297250 Apr 8 '10 at 22:45
    
I've got a neat use case - a readme file, with examples in it, which I'd like doctest to validate. I'm hoping to make a doctest markdown doc that works... – Danny Staple Nov 24 '11 at 23:05
    
And the answer is (for that use case) - use doctest.loadfile! – Danny Staple Nov 24 '11 at 23:10
    
The issue with wrappers is that if someone naively copies just the wrapper to a bin/ directory, the program won't work when run from the path. – Curt J. Sampson Nov 22 '17 at 1:14

imp.load_source(module_name, path) should do or you can do the more verbose imp.load_module(module_name, file_handle, ...) route if you have a file handle instead

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.