What's the difference between a Python module and a Python package?

What's the difference between a Python module and a Python package? See also: What's the difference between "package" and "module" (for other languages) A module is a single file (or files) that are imported under one import and used. eg import my_module A package is a collection of modules in directories that give a package hierarchy. from my_package.timing.da

Python模块和Python包有什么区别?

Python模块和Python包有什么区别? 另请参阅:“包”和“模块”之间的区别(对于其他语言) 模块是一个文件(或多个文件),它们在一个导入下导入并使用。 例如 import my_module 包是一个包含层次结构的目录中的模块集合。 from my_package.timing.danger.internets import function_of_love 模块文档 软件包简介 任何Python文件都是一个模块,其名称是文件的基本名称,不带.py扩展名。 一个包是一个Python模块的集合

Importing files from different folder

I have the following folder structure. application/app/folder/file.py and I want to import some functions from file.py in another Python file which resides in application/app2/some_folder/some_file.py I've tried from application.app.folder.file import func_name and some other various attempts but so far I couldn't manage to import properly. How can I do this? By default, yo

从不同文件夹导入文件

我有以下文件夹结构。 application/app/folder/file.py 我想从驻留在另一个Python文件中的file.py导入一些函数 application/app2/some_folder/some_file.py 我试过了 from application.app.folder.file import func_name 和其他一些尝试,但到目前为止我无法正常导入。 我怎样才能做到这一点? 默认情况下,你不能。 在导入文件时,Python只搜索当前目录,运行入口点脚本的目录以及包含软件包安装目录等位置的sy

Running python package as a module

This question already has an answer here: What is __init__.py for? 11 answers What is __main__.py? 5 answers

将python包作为模块运行

这个问题在这里已经有了答案: 什么是__init__.py? 11个答案 什么是__main__.py? 5个答案

Why eclipse creates two class files for Python?

This question already has an answer here: What is __init__.py for? 11 answers Placing __init__.py file in a folder simply tells the interpreter/IDE that it contain(may contain) python scrips. Now in the mentioned scenario, eclipse placed two __init__.py files but one for/in com folder and one for myPythonProject . Which means both folders contain(may contain) python files.

为什么eclipse为Python创建两个类文件?

这个问题在这里已经有了答案: 什么是__init__.py? 11个答案 将__init__.py文件放在文件夹中只是告诉解释器/ IDE它包含(可能包含)python脚本。 现在在上面提到的场景中,eclipse放置了两个__init__.py文件,其中一个用于com文件夹中的/,另一个用于myPythonProject 。 这意味着两个文件夹都包含(可能包含)python文件。

ImportError: No module named application

This question already has an answer here: What is __init__.py for? 11 answers I think that with Python 2, you will have to put an empty __init__.py file in your database directory First your database folder isn't a proper python package. Each package in Python is a directory which MUST contain a special file called __init__.py. This file can be empty, and it indicates that the di

ImportError:没有名为application的模块

这个问题在这里已经有了答案: 什么是__init__.py? 11个答案 我认为在Python 2中,你将不得不放空 __init__.py 文件在您的数据库目录中 首先你的数据库文件夹不是一个合适的python包。 Python中的每个包都是一个必须包含名为__init__.py.的特殊文件的目录__init__.py. 这个文件可以是空的,它表明它所包含的目录是一个Python包,因此可以像导入模块一样导入它。 从学习python网站 其次(这只是一个建议),为了

What is the purpose of the

This question already has an answer here: What is __init__.py for? 11 answers It a signal to Python that the folder is a package, not just a folder. It also contains initialization code that is run when the package is import ed into a script. See the docs on the subject for more. The most relevant extract: The __init__.py files are required to make Python treat the directories as conta

这个的目的是什么?

这个问题在这里已经有了答案: 什么是__init__.py? 11个答案 这是一个信号给Python,该文件夹是一个包,而不仅仅是一个文件夹。 它还包含在程序包import脚本时运行的初始化代码。 有关更多信息,请参阅有关该主题的文档。 最相关的摘录: 需要__init__.py文件才能使Python将目录视为包含包; 这是为了防止具有通用名称的目录(例如string )无意中隐藏稍后在模块搜索路径中发生的有效模块。 在最简单的情况下, __i

Why do I need

This question already has an answer here: What is __init__.py for? 11 answers Yes, this file is required if you want directory to be treated as module. The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the modul

我为什么需要

这个问题在这里已经有了答案: 什么是__init__.py? 11个答案 是的,如果您要将目录视为模块,则需要此文件。 需要__init__.py文件才能使Python将目录视为包含包; 这是为了防止具有通用名称的目录(例如字符串)无意中隐藏稍后在模块搜索路径中发生的有效模块。 在最简单的情况下, __init__.py可以只是一个空文件,但它也可以执行包的初始化代码或设置__all__变量,稍后介绍。 https://docs.python.org/3/tutorial/m

What is the reason python needs

This question already has an answer here: What is __init__.py for? 11 answers The documentation is very clear on this - your project structure could look like this: app - common - init.py - resources - string - src If Python implicitly treated the directories as packages, the "string" directory could present a name clash with Python's built-in string module (https://doc

python需要什么原因

这个问题在这里已经有了答案: 什么是__init__.py? 11个答案 文档非常清晰 - 您的项目结构可能如下所示: app - common - init.py - resources - string - src 如果Python的隐式处理的目录作为包,将“串”目录可能出现名称冲突与Python的内置字符串模块(https://docs.python.org/2/library/string.html)。 这意味着在调用import string ,模块不明确。 __init__.py还增加了一些功能:初始化包时执行代码,因此

Why compile Python code?

Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? I also notice that some files in my application get compiled into .pyc while others do not, why is this? It's compiled to bytecode which can be used much, much, much faster. The reason some files aren't compiled is that the main scr

为什么要编译Python代码?

你为什么要编译一个Python脚本? 你可以直接从.py文件运行它们,它可以正常工作,所以有一个性能优势或者什么? 我还注意到,我的应用程序中的某些文件被编译为.pyc,而其他文件则不会,为什么? 它被编译为字节码,可以更快速地使用这些字节码。 某些文件未编译的原因是,每次运行脚本时,使用python main.py调用的主脚本都会重新编译。 所有导入的脚本将被编译并存储在磁盘上。 Ben Blank重要补充: 值得注意的是

how do imports work for a module used as a singleton?

I'm confused about what happens if you tread a module as a singleton. Say I have a module conf.py, that contains some configuration parameters which need to be accessed by multiple other files. In conf.py, I might have this piece of code (and nothing else): myOption = 'foo' + 'bar' If I now import it first in a.py, and then in b.py, my understanding is that the first time it is imported

导入如何用作用作单例模块的​​模块?

如果您将模块作为单例使用,会发生什么情况,我很困惑。 假设我有一个conf.py模块,其中包含一些需要由多个其他文件访问的配置参数。 在conf.py中,我可能有这段代码(而没有其他): myOption = 'foo' + 'bar' 如果我现在先在a.py中导入它,然后在b.py中导入,我的理解是第一次导入时(在a.py中),字符串连接将被执行。 但是第二次导入时(在b.py中),conf.myOption已经有了它的值,所以不会执行字符串连接。 它是否正