When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
答复
(或者只是关于这个问题的一些信息)
根据python docs,导入模块的工作方式如下:
因此,如果您只是想}返回的列表中。要实现这一点,有三种方法(据我所知):
import
一个模块,例如my_pkg.my_module.py
,它位于与导入它的模块不同的位置,那么my_pkg
应该在{PYTHONPATH
变量。(正如所指出的,here)-不首选sys.path
。(正如所指出的,here)-不首选setup.py
文件,用于解析模块的导入依赖项(并执行许多其他重要操作)-正确的方式因此,上面的前两种方法通常被认为不是一种好的实践,而是一种暂时的坏的解决方案,在开发过程中会导致进一步的问题。然而,第三种方法是构建一个易于被其他人使用的、组织良好的、可分发的项目的最佳方法。
setup.py
文件的一些好处以及它的全部功能都在here中进行了说明使用
setup.py
文件,您可以清楚地配置,这些包包含要导入以解决ImportError
问题的模块,并对您的项目执行许多其他有用的操作下面引用了两个非常具有说明性的教程,介绍了如何使用导入语句和如何创建
setup.py
文件,我强烈建议看看:编辑:此外,您可以在此处找到有关如何配置、打包和分发项目的大量信息:
就我个人而言,为了构建一个非常好的第一个
setup.py
文件,我发现它非常有用相关问题 更多 >
编程相关推荐