- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 345字
- 2021-07-02 21:09:38
Turning a regular folder into a package
There are two things that turn a regular folder into a package. These are explained as follows:
The first is where is it that is, the location of the folder. Python only looks in certain places for packages and if your folder isn't in the right place, Python won't notice it.
The sys.path variable contains the list of all the places Python will look for packages. The sys.path variable is fairly sparse, but user configuration can make it much more extensive as shown in the following screenshot:

Notice that the first entry in the list is an empty string. This stands for the current working directory.
We can change the current working directory with the cd command:
cd demopackage cd ..
The cd.. command in the preceding code means go back to the previous directory or the parent directory. So, in this case, I went in and out of demopackage.
The fact that the current working directory is in the path is convenient during development; it means that we could just set the current working directory to the place where we're doing our development and all of our packages become available, at least, as long as we also use the command line to launch Python.
The second thing that turns a regular folder into a package is the presence of __init__.py file, though not strictly necessary since Python 3.3 and beyond. An init file marks the folder as a package, which makes it load more efficiently and also gives us a place to put information and code relevant to the interface of the package as a whole.
While it's quite common that the __init__.py file is completely empty and serves only as a marker, there's one language feature that won't be supported unless we add a little code to the file.
This feature is the ability to import all the package's modules using the import* syntax, as shown here:

- INSTANT Mock Testing with PowerMock
- WebAssembly實(shí)戰(zhàn)
- Practical UX Design
- 構(gòu)建移動(dòng)網(wǎng)站與APP:HTML 5移動(dòng)開(kāi)發(fā)入門(mén)與實(shí)戰(zhàn)(跨平臺(tái)移動(dòng)開(kāi)發(fā)叢書(shū))
- Raspberry Pi Networking Cookbook(Second Edition)
- C語(yǔ)言課程設(shè)計(jì)
- Node Cookbook(Second Edition)
- Canvas Cookbook
- C++ Application Development with Code:Blocks
- C語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn)教程與水平考試指導(dǎo)
- Oracle 12c從入門(mén)到精通(視頻教學(xué)超值版)
- 數(shù)據(jù)結(jié)構(gòu)與算法詳解
- jQuery Essentials
- Test-Driven Java Development(Second Edition)
- jMonkeyEngine 3.0 Cookbook