尝试导入时可以在Python文件中使用破折号吗? [英] Is it ok to use dashes in Python files when trying to import them?

查看:115
本文介绍了尝试导入时可以在Python文件中使用破折号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Basically when I have a python file like:

python-code.py

并使用:

import (python-code)

解释器给我语法错误.

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

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

推荐答案

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

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

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

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.

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

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

这篇关于尝试导入时可以在Python文件中使用破折号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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