简体   繁体   English

Python导入:导入没有.py扩展名的模块?

[英]Python imports: importing a module without .py extension?

In a Python system for which I develop, we usually have this module structure. 在我开发的Python系统中,我们通常有这个模块结构。

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. 这允许我们的小测试框架轻松导入test / feature.py并运行单元测试。 However, we now have the need for some shell scripts (which are written in Python): 但是,我们现在需要一些shell脚本(用Python编写):

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

yetanotherfeature.py is installed by the module Debian package into /usr/bin. yetanotherfeature.py由模块Debian软件包安装到/ usr / bin中。 But we obviously don't want the .py extension there. 但我们显然不希望扩展.py扩展。 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? 是否可以在Python中通过文件名导入模块,或者您能想到更优雅的解决方案吗?

The imp module is used for this: imp模块用于此:

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$ 

Check out imp module: 查看imp模块:

http://docs.python.org/library/imp.html 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. 但我认为你的符号链接是一个更优雅的解决方案。

You could most likely use some tricker by using import hooks , I wouldn't recommend it though. 你最有可能通过使用导入钩子来使用一些tricker,但我不推荐它。 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. 另一方面,我也可能会以相反的方式执行此操作,将.py脚本放在某处,并为.py文件创建“.py”无符号链接。 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. 所以你的库可以在任何地方,你可以从内部运行测试,通过导入它normall(因为它有py扩展名),然后/ usr / bin / yetanotherfeature指向它,所以你可以在没有py的情况下运行它。

Edit: Nevermind this (at least the hooks part), the import imp solution looks very good to me :) 编辑:没关系这个(至少是钩子部分),导入imp解决方案看起来对我很好:)

Another option would be to use setuptools: 另一种选择是使用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..." “...没有简单的方法让脚本的文件名与Windows和POSIX平台上的本地约定相匹配。另一方面,当你的实际”主“是一个时,你经常需要为”主“脚本创建一个单独的文件。在某个模块中运行的函数... setuptools通过使用正确的扩展名为您自动生成脚本来修复所有这些问题,并且在Windows上它甚至会创建一个.exe文件...“

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM