简体   繁体   中英

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

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

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

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.

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.py into foo/ , then you can do

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.

a package is a collector for files that have a logical grouping

import <package>.<file>

and a source folder makes the files directly importable

import <file>

and the regular folder is 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()

在此输入图像描述

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:

### 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()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM