问题:尝试导入时可以在Python文件中使用破折号吗?

基本上,当我有一个类似python的文件时:

python-code.py

并使用:

import (python-code)

解释器给我语法错误。

关于如何解决它的任何想法?python文件名中的破折号非法吗?

Basically when I have a python file like:

python-code.py

and use:

import (python-code)

the interpreter gives me syntax error.

Any ideas on how to fix it? Are dashes illegal in python file names?


回答 0

您应该查看PEP 8(Python代码样式指南):

程序包和模块名称模块应使用简短的全小写名称。如果模块名称可以提高可读性,则可以在模块名称中使用下划线。尽管不鼓励使用下划线,但Python软件包也应使用短小写全名。

由于模块名称被映射为文件名,并且某些文件系统不区分大小写,并且截断长名称,因此,将模块名称选择为相当短是很重要的-在Unix上这不是问题,但可能是将代码传输到较旧的Mac或Windows版本或DOS时出现问题。

换句话说:重命名文件:)

You should check out PEP 8, the Style Guide for Python Code:

Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short — this won’t be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.

In other words: rename your file :)


回答 1

代码中需要注意的另一件事是导入不是一个函数。所以import(python-code)应该是import python-code,正如一些已经提到,被解释为“进口Python减码”,并非您的本意。如果确实需要导入名称中带有短划线的文件,则可以执行以下操作:

python_code = __import__('python-code')

但是,如上所述,这并不是真正的建议。如果要控制文件名,则应更改文件名。

One other thing to note in your code is that import is not a function. So import(python-code) should be import python-code which, as some have already mentioned, is interpreted as “import python minus code”, not what you intended. If you really need to import a file with a dash in its name, you can do the following::

python_code = __import__('python-code')

But, as also mentioned above, this is not really recommended. You should change the filename if it’s something you control.


回答 2

TLDR

短划线不是非法的,但出于以下三个原因,您不应使用它们:

  1. 您需要特殊的语法来导入带有破折号的文件
  2. 没有人期望带有破折号的模块名称
  3. 这违反了Python样式指南的建议

如果您确实需要导入带短划线的文件名,则特殊语法为:

module_name = __import__('module-name')

好奇为什么我们需要特殊的语法?

使用特殊语法的原因是,在编写代码时,import somename您正在创建带有标识符的模块对象somename(因此以后可以将其与eg一起使用somename.funcname)。当然module-name不是有效的标识符,因此是一种有效的特殊语法。

您不明白为什么模块名不是有效的标识符?

不用担心-我也没有。这里有一条提示可以帮助您:查看此python行:x=var1-var2。您看到赋值右侧的减法还是带有破折号的变量名?

聚苯乙烯

除了将我认为是所有其他答案中最相关的信息包括在一个地方之外,我的答案没有任何原始内容

TLDR

Dashes are not illegal but you should not use them for 3 reasons:

  1. You need special syntax to import files with dashes
  2. Nobody expects a module name with a dash
  3. It’s against the recommendations of the Python Style Guide

If you definitely need to import a file name with a dash the special syntax is this:

module_name = __import__('module-name')

Curious about why we need special syntax?

The reason for the special syntax is that when you write import somename you’re creating a module object with identifier somename (so you can later use it with e.g. somename.funcname). Of course module-name is not a valid identifier and hence the special syntax that gives a valid one.

You don’t get why module-name is not valid identifier?

Don’t worry — I didn’t either. Here’s a tip to help you: Look at this python line: x=var1-var2. Do you see a subtraction on the right side of the assignment or a variable name with a dash?

PS

Nothing original in my answer except including what I considered to be the most relevant bits of information from all other answers in one place


回答 3

问题是这python-code不是标识符。解析器将其视为python减号code。当然,这不会满足您的要求。您将需要使用也是有效的python标识符的文件名。尝试用-下划线代替。

The problem is that python-code is not an identifier. The parser sees this as python minus code. Of course this won’t do what you’re asking. You will need to use a filename that is also a valid python identifier. Try replacing the - with an underscore.


回答 4

您可能可以通过一些__import__黑客手段将其导入,但是如果您还不知道如何操作,则不应该这样做。Python模块名称应该是有效的变量名称(“标识符”)-这意味着,如果您有一个模块foo_bar,则可以在Python(print foo_bar)中使用它。您将无法使用一个怪异的名称来执行此操作(print foo-bar->语法错误)。

You could probably import it through some __import__ hack, but if you don’t already know how, you shouldn’t. Python module names should be valid variable names (“identifiers”) — that means if you have a module foo_bar, you can use it from within Python (print foo_bar). You wouldn’t be able to do so with a weird name (print foo-bar -> syntax error).


回答 5

在Python 3上,使用import_module

from importlib import import_module
python_code = import_module('python-code')

更普遍,

import_module('package.subpackage.module')

On Python 3 use import_module:

from importlib import import_module
python_code = import_module('python-code')

More generally,

import_module('package.subpackage.module')

回答 6

尽管正确的文件命名是最好的方法,但是如果python-code不受我们的控制,使用hack __import__比复制,重命名或以其他作者的代码搞乱为佳。但是,我尝试了一下,除非重新命名了添加.py扩展名的文件,否则它不起作用。在查看 了文档以获取如何获取的描述之后.py,我得出了以下结论

import imp

try:
    python_code_file = open("python-code")
    python_code = imp.load_module('python_code', python_code_file, './python-code', ('.py', 'U', 1))
finally:
    python_code_file.close()

python-codec在第一次运行时创建了一个新文件。

Although proper file naming is the best course, if python-code is not under our control, a hack using __import__ is better than copying, renaming, or otherwise messing around with other authors’ code. However, I tried and it didn’t work unless I renamed the file adding the .py extension. After looking at the doc to derive how to get a description for .py, I ended up with this:

import imp

try:
    python_code_file = open("python-code")
    python_code = imp.load_module('python_code', python_code_file, './python-code', ('.py', 'U', 1))
finally:
    python_code_file.close()

It created a new file python-codec on the first run.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。