PyDev Eclipse 中的“源文件夹”和“pydev 包”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18546387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:08:18  来源:igfitidea点击:

What is the difference between a "source folder" and a "pydev package" in PyDev Eclipse?

pythoneclipsepackagepydev

提问by alwbtc

What is the difference between a "source folder" and a "pydev package" in PyDev Eclipse?

PyDev Eclipse 中的“源文件夹”和“pydev 包”有什么区别?

Menu options for creating new item in PyDev

在 PyDev 中创建新项目的菜单选项

采纳答案by Michael Herrmann

A "source folder" is a directory that contains source files. Putting .py files into this directory will make them discoverable by PyDev, so that you can for instance import them from other Python files.

“源文件夹”是包含源文件的目录。将 .py 文件放入此目录将使 PyDev 可以发现它们,例如,您可以从其他 Python 文件中导入它们。

A "PyDev Package" is a Python package. This means that it contains a file called __init__.py. For example, if you create a new PyDev Package with name foo, then you will get file foo/__init__.py. You can place other .py files into foo/, which you can then import. So, if you place bar.pyinto foo/, then you can do

“PyDev 包”是一个 Python 包。这意味着它包含一个名为__init__.py. 例如,如果您创建一个名为 name 的新 PyDev 包foo,那么您将获得 file foo/__init__.py。您可以将其他 .py 文件放入 中foo/,然后您可以将其导入。所以,如果你bar.py放入foo/,那么你可以做

import foo.bar

This is not possible with source folders.

这对于源文件夹是不可能的。

You normally place packages into source folders. I don't know if it is possible to place a source folder into a package, but even if it were you would hardly ever do it.

您通常将包放入源文件夹中。我不知道是否可以将源文件夹放入包中,但即使是这样,您也几乎不会这样做。

回答by anregen

a packageis a collector for files that have a logical grouping

一个是用于具有逻辑分组文件的集电极

import <package>.<file>

and a source foldermakes the files directly importable

源文件夹使文件可直接导入

import <file>

and the regular folderis basically inaccessible.

普通文件夹基本上是无法访问的。

I made a quick project that shows the differences. I put one file in each type of container: regular folder, package, and source folder. Each file had two items: a function called show() and a class with a single public member show()

我做了一个显示差异的快速项目。我在每种类型的容器中放置一个文件:常规文件夹源文件夹。每个文件都有两个项目:一个名为 show() 的函数和一个具有单个公共成员 show() 的类

enter image description here

在此处输入图片说明

I then put a driver file at the top level (in the project root, next to the 3 containers). This was just to try the different ways of importing things. Here is a copy of that driver file with comments to describe how the different elements are used:

然后我将一个驱动程序文件放在顶层(在项目根目录中,在 3 个容器旁边)。这只是为了尝试不同的导入方式。这是该驱动程序文件的副本,其中包含用于描述如何使用不同元素的注释:

### valid imports
import package
import package.file_in_package as thefileinpackage
import file_in_source

### invalid imports
#import package.file_in_package.packageclass   #runtime ImportError
#import file_in_package                        #unresolved import

#import source                                 #unresolved import
#import source.file_in_source                  #unresolved import
#import file_in_source.sourceclass             #runtime ImportError

#import folder                                 #unresolved import
#import file_in_folder                         #unresolved import
#import folder.file_in_folder                  #unresolved import

thefileinpackage.show()
packageclasss_inst = thefileinpackage.packageclass()
packageclasss_inst.show()

file_in_source.show()
sourceclass_inst = file_in_source.sourceclass()
sourceclass_inst.show()

package.file_in_package.show()
packageclass_inst2 = package.file_in_package.packageclass()
packageclass_inst2.show()

回答by Fabio Zadrozny

A source folder is the folder which is added to the PYTHONPATH.

源文件夹是添加到 PYTHONPATH 的文件夹。

A package is a folder which has an __init__.pyfile (and which is located beneath a source folder).

包是一个文件夹,其中包含一个__init__.py文件(位于源文件夹下方)。