导入模块具有非标准文件名 [英] Import module with non-standard file name

查看:45
本文介绍了导入模块具有非标准文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们好,


问题:我的Python模块名称中没有''。''作为扩展,

我想要能够导入它们。我怎么能这样做?


背景:

在Unix上,我编写的程序旨在作为文件的命令运行

没有扩展名。这允许其他程序将该命令用作

接口,并且我可以用其他语言重新编写程序

而不会废弃命令行界面。


例如,我可能会把''frobnicate-foo''写成一个shell程序,这样其他

程序就可以''frobnicate-foo --bar baz''。如果我后来决定在Python中重新实现''frobnicate-foo'',我会将顶级

模块保存到相同的文件名中,因为它实现相同的命令行

界面。


既然我已经把它写成了Python模块,我想写

该模块的单元测试,当然需要导入

程序模块来测试它。单元测试可以明确地将程序模块所在的

目录添加到''sys.path''用于导入该模块的



然而,Python引用告诉我''import''(具体来说,

带有''.py''后缀。但是我的模块位于一个名为

''frobnicate-foo''的文件中,没有后缀,而且这是程序界面定义

的一部分。


我不想要符号链接,或其他任何为同一模块提供两个

文件名的文件名,因为没有必要除了
,因为Python明显坚持特定命名

约定。此外,避免源代码树中的符号链接

使版本控制更加顺畅。

从名称不能
改变了吗?


-

\[W] e仍然是第一代用户,而且对于所有这些用户而言|

` \我们可能已经发明了网,我们仍然没有真正得到它。 |

_o__) - 道格拉斯亚当斯|

Ben Finney

Howdy all,

Question: I have Python modules named without ''.py'' as the extension,
and I''d like to be able to import them. How can I do that?

Background:

On Unix, I write programs intended to be run as commands to a file
with no extension. This allows other programs to use the command as an
interface, and I can re-write the program in some other language
without obsoleting the commandline interface.

e.g., I might write ''frobnicate-foo'' as a shell program so that other
programs can ''frobnicate-foo --bar baz''. If I later decide to
re-implement ''frobnicate-foo'' in Python, I''ll save the top level
module to the same file name since it implements the same command-line
interface.

Now that I''ve got it written as a Python module, I''d like to write
unit tests for that module, which of course will need to import the
program module to test it. The unit test can explicitly add the
directory where the program module lives to ''sys.path'' for the purpose
of importing that module.

However, the Python reference tells me that ''import'' (specifically,
''__import__()'') needs modules to live in files named a particular way:
with a ''.py'' suffix. But my module is in a file called
''frobnicate-foo'', with no suffix, and that''s part of the definition of
the program interface.

I don''t want symbolic links, or anything else that presents two
filenames for the same module, because there''s no need for that except
for Python''s apparent insistence on a particular naming
convention. Also, avoiding symbolic links inside the source code tree
makes version control smoother.
What are my options to import a module from a file whose name can''t
change?

--
\ "[W]e are still the first generation of users, and for all that |
`\ we may have invented the net, we still don''t really get it." |
_o__) -- Douglas Adams |
Ben Finney

推荐答案



Ben Finney写道:

Ben Finney wrote:

你好,


问题:我有Python模块命名没有''.py''作为扩展名,

我希望能够导入它们。我怎么能这样做?


背景:

在Unix上,我编写的程序旨在作为文件的命令运行

没有扩展名。这允许其他程序将该命令用作

接口,并且我可以用其他语言重新编写程序

而不会废弃命令行界面。


例如,我可能会把''frobnicate-foo''写成一个shell程序,这样其他

程序就可以''frobnicate-foo --bar baz''。如果我后来决定在Python中重新实现''frobnicate-foo'',我会将顶级

模块保存到相同的文件名中,因为它实现相同的命令行

界面。


既然我已经把它写成了Python模块,我想写

该模块的单元测试,当然需要导入

程序模块来测试它。单元测试可以明确地将程序模块所在的

目录添加到''sys.path'',以便导入该模块。
导入该模块。
Howdy all,

Question: I have Python modules named without ''.py'' as the extension,
and I''d like to be able to import them. How can I do that?

Background:

On Unix, I write programs intended to be run as commands to a file
with no extension. This allows other programs to use the command as an
interface, and I can re-write the program in some other language
without obsoleting the commandline interface.

e.g., I might write ''frobnicate-foo'' as a shell program so that other
programs can ''frobnicate-foo --bar baz''. If I later decide to
re-implement ''frobnicate-foo'' in Python, I''ll save the top level
module to the same file name since it implements the same command-line
interface.

Now that I''ve got it written as a Python module, I''d like to write
unit tests for that module, which of course will need to import the
program module to test it. The unit test can explicitly add the
directory where the program module lives to ''sys.path'' for the purpose
of importing that module.



如果可以这样做,它可以将MUT复制到某个临时目录,在名称的末尾添加

..py新文件,并将临时目录

放在sys.path中....不可以吗?


干杯,

John

If it can do that, it can copy the MUT to some temp directory, adding
..py to the end of the name of the new file, and put the temp directory
in sys.path .... can''t it?

Cheers,
John


" John Machin" < sj ****** @ lexicon.netwrites:
"John Machin" <sj******@lexicon.netwrites:

Ben Finney写道:
Ben Finney wrote:

既然我已经把它写成了Python模块,我想为该模块编写

单元测试,当然这需要导入

程序模块来测试它。单元测试可以明确地将程序模块所在的目录添加到''sys.path'',以便导入该模块。

目的。
Now that I''ve got it written as a Python module, I''d like to write
unit tests for that module, which of course will need to import
the program module to test it. The unit test can explicitly add
the directory where the program module lives to ''sys.path'' for the
purpose of importing that module.



如果它可以做到这一点,它可以将MUT复制到某个临时目录,

将.py添加到名称的末尾新文件,并将temp

目录放在sys.path中....不可以吗?


If it can do that, it can copy the MUT to some temp directory,
adding .py to the end of the name of the new file, and put the temp
directory in sys.path .... can''t it?



听起来像一个令人讨厌的黑客(不是那个摆弄的系统路径不是黑客,但是

至少那个''在Python 2.5中用相对和绝对

导入来解决。



$ b $中的文件导入程序模块的问题b不同的目录是该程序无法找到自己的

相关模块。这导致* more * sys.path hackery,或者

从* same *目录中的临时文件导入。


除此之外,那个'仍然是同一模块的两个文件名

代码。测试的全部意义在于知道我正在测试相同的

模块;使用双文件垫片,距离

理想距离更近一步。


你所描述的是可能的,但会导致非常臭黑客。我会想要看看有什么其他选择。


-

\它'''不是你付给男人的钱,而是他花了你多少钱?

` \计数。 - Will Rogers |
_o__)|

Ben Finney

Sounds like a nasty hack (not that fiddling sys.path isn''t a hack, but
at least that one''s addressed in Python 2.5 with relative and absolute
imports).

The problem with importing the program module from a file in a
different directory is that the program won''t be able to find its own
relative modules. That leads to either *more* sys.path hackery, or
importing from a temporary file in the *same* directory.

Besides which, that''s still two file names for the same module
code. The whole point of testing is to know that I''m testing the same
module; with a two-file shim, that''s one step further away from that
ideal.

What you describe is possible, but leads to very smelly hacks. I''d
like to see what other options there are.

--
\ "It''s not what you pay a man, but what he costs you that |
`\ counts." -- Will Rogers |
_o__) |
Ben Finney




Ben Finney写道:

Ben Finney wrote:

" John Machin" < sj ****** @ lexicon.netwrites:
"John Machin" <sj******@lexicon.netwrites:

Ben Finney写道:
Ben Finney wrote:

既然我已经把它写成了Python模块,我想为该模块编写

单元测试,当然这需要导入

程序模块来测试它。单元测试可以明确地将程序模块所在的目录添加到''sys.path'',以便导入该模块。

目的。
Now that I''ve got it written as a Python module, I''d like to write
unit tests for that module, which of course will need to import
the program module to test it. The unit test can explicitly add
the directory where the program module lives to ''sys.path'' for the
purpose of importing that module.



如果可以这样做,它可以将MUT复制到某个临时目录,

将.py添加到名称的末尾新文件,并将temp

目录放在sys.path中....不可以吗?

If it can do that, it can copy the MUT to some temp directory,
adding .py to the end of the name of the new file, and put the temp
directory in sys.path .... can''t it?



听起来像一个令人讨厌的黑客(不是那个摆弄的系统路径不是黑客,但

至少那个''在Python 2.5中用相对和绝对

导入来解决。



$ b $中的文件导入程序模块的问题b不同的目录是该程序无法找到自己的

相关模块。这导致* more * sys.path hackery,或

从* same *目录中的临时文件导入。


Sounds like a nasty hack (not that fiddling sys.path isn''t a hack, but
at least that one''s addressed in Python 2.5 with relative and absolute
imports).

The problem with importing the program module from a file in a
different directory is that the program won''t be able to find its own
relative modules. That leads to either *more* sys.path hackery, or
importing from a temporary file in the *same* directory.



请解释自己和自己。和相对的在它自己的亲戚

模块中。这些模块的名称是否以.py结尾?

Please explain both the "own" and "relative" in "its own relative
modules". Do these modules not have names that end in ".py"?


>

除此之外,那还是两个同一模块的文件名

代码。测试的全部意义在于知道我正在测试相同的

模块;使用双文件垫片,距离

理想距离更近一步。
>
Besides which, that''s still two file names for the same module
code. The whole point of testing is to know that I''m testing the same
module; with a two-file shim, that''s one step further away from that
ideal.



双文件caper仅在测试期间存在。你将会b / b
必须信任你自己编写和测试文件复制小工具。 :-)

The two-file caper exists only for the duration of the test. You''ll
have to trust yourselt to write and test a file copying gadget. :-)


>

你所描述的是可能的,但会导致非常臭的黑客攻击。我会

想看看有什么其他选择。
>
What you describe is possible, but leads to very smelly hacks. I''d
like to see what other options there are.



可能更有气味的......:<)


干杯,

John

Probably smellier ones ...:<)

Cheers,
John


这篇关于导入模块具有非标准文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆