4

In a Python system for which I develop, we usually have this module structure.

mymodule/
mymodule/mymodule/feature.py
mymodule/test/feature.py

This allows our little testing framework to easily import test/feature.py and run unit tests. However, we now have the need for some shell scripts (which are written in Python):

mymodule/
mymodule/scripts/yetanotherfeature.py
mymodule/test/yetanotherfeature.py

yetanotherfeature.py is installed by the module Debian package into /usr/bin. But we obviously don't want the .py extension there. So, in order for the test framework to still be able to import the module I have to do this symbolic link thingie:

mymodule/
mymodule/scripts/yetanotherfeature
mymodule/scripts/yetanotherfeature.py @ -> mymodule/scripts/yetanotherfeature
mymodule/test/yetanotherfeature.py

Is it possible to import a module by filename in Python, or can you think of a more elegant solution for this?

pojo
  • 5,657
  • 7
  • 31
  • 46
  • 2
    What's wrong with the symbolic link? This is the most elegant way to handle this; you should use it for deployment, also. /usr/bin/feature is a link to /usr/bin/feature.py works nicely by making the implementation clear. – S.Lott May 19 '09 at 10:40

4 Answers4

9

The imp module is used for this:

daniel@purplehaze:/tmp/test$ cat mymodule
print "woho!"
daniel@purplehaze:/tmp/test$ cat test.py 
import imp
imp.load_source("apanapansson", "mymodule")
daniel@purplehaze:/tmp/test$ python test.py
woho!
daniel@purplehaze:/tmp/test$ 
dsvensson
  • 1,301
  • 11
  • 19
  • 3
    This creates a file called "mymodulec". It's supposed to be the .pyc, but since the original file didn't end in .py, it just tacks a c on the end. It's weird-looking. Can I make it not do this? – Nick Retallack Jun 16 '09 at 03:52
  • If you don't want "mymodulec" to be created, set `sys.dont_write_bytecode = True` – Todd Owen Jul 17 '12 at 03:33
  • Note that imp.load_source is obsolete, but still in python 2.7. For python 3.3, see [Import abitrary python source file. (Python 3.3+) - Stack Overflow](http://stackoverflow.com/questions/19009932/import-abitrary-python-source-file-python-3-3). For more clarity on what the name argument ("apanapansson" here) is for, see [python - loading modules by imp.load_source with same name resulting merger of the modules - Stack Overflow](http://stackoverflow.com/questions/15082857/loading-modules-by-imp-load-source-with-same-name-resulting-merger-of-the-module). – nealmcb Oct 04 '14 at 16:39
2

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 :)

Nico
  • 1,328
  • 8
  • 7
  • Actually I think that probably makes the most sense. The current design feels a bit flawed :) – pojo May 19 '09 at 09:22
1

Check out imp module:

http://docs.python.org/library/imp.html

This will allow you to load a module by filename. But I think your symbolic link is a more elegant solution.

Unknown
  • 43,382
  • 24
  • 132
  • 174
0

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

user2745509
  • 191
  • 2
  • 3