Records using simple Python modules and packages.

For the package to the module definition: http: //www.imooc.com/article/247114 written here is good.

 

This reference link is also very good, I said very clearly why the relative path can not be executed directly in the form of scripts.

https://www.jianshu.com/p/04bac02ae3f0

 

Before using Python's standard library import XXX

Or from xxx import xxx

In fact, there are specific techniques to use at no systematic study had been that can be used to ok

Arrived in the Python cookbook there are relatively detailed introduction, I picked some simple some records to yourself.

First Python has the Python environment variable, we can import from outside or from the Python environment variables are there, here is my IPython environment variable.

In [5]: sys.path                                                                                   
Out[5]: 
['/usr/local/bin',
 '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '',
 '/usr/local/lib/python3.7/site-packages',
 '/usr/local/lib/python3.7/site-packages/IPython/extensions',
 '/Users/shijianzhong/.ipython']

 Python has a very interesting tip, is that when you run a file, that file folder will join PythonPATH inside.

shijianzhongdeMacBook-Pro:formats shijianzhong$ python3 jpg.py 
['/Users/shijianzhong/study/PythonCookbook/charter_10/t_10_1_2/graphics/formats', 
'/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages'] shijianzhongdeMacBook-Pro:formats shijianzhong$ pwd /Users/shijianzhong/study/PythonCookbook/charter_10/t_10_1_2/graphics/formats

 This is one of my comparison of the files inside, he was his own folder (which is a top-level folder) as a package and put it into the Python environment variable.

This folder contains all packages, and a module can be introduced below.

I Pycharm created a new working paper, Pycharm default settings will work this directory to the Python environment, I was

/Users/shijianzhong/study/PythonCookbook/charter_10/t_10_1_2

The following is the contents of the file:

└── graphics
    ├── __init__.py
    ├── __pycache__
    │   └── __init__.cpython-37.pyc
    ├── formats
    │   ├── __init__.py
    │   ├── jpg.py
    │   └── png.py
    └── primitive
        ├── __init__.py
        ├── __pycache__
        │   └── __init__.cpython-37.pyc
        ├── fill.py
        ├── line.py
        └── text.py

 First of all I am going to pass this case to simply explain.

In [1]: import sys                                                                                 

In [2]: sys.path                                                                                   
Out[2]: 
['/usr/local/bin',
 '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '',
 '/usr/local/lib/python3.7/site-packages',
 '/usr/local/lib/python3.7/site-packages/IPython/extensions',
 '/Users/shijianzhong/.ipython']

 Obviously, ipython environment inside the python did not I just the new working directory path.

In [7]: cd ~                                                                                       
/Users/shijianzhong

In [8]: from graphics import primitive                                                             
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-8-e5e5ddc40003> in <module>
----> 1 from graphics import primitive

ModuleNotFoundError: No module named 'graphics'

 Obviously, this environment because there is no path to guide the package to fail. Then add the path:

In [9]: sys.path.append('/Users/shijianzhong/study/PythonCookbook/charter_10/t_10_1_2')            

In [10]: from graphics import primitive                                                            

In [11]: primitive                                                                                 
Out[11]: <module 'graphics.primitive' from '/Users/shijianzhong/study/PythonCookbook/charter_10/t_10_1_2/graphics/primitive/__init__.py'>

 Add the path environment variables, all of this work directory module can only be imported to the package.

In order to test whether the module can import, I created a new file in the top-level directory (t.py):

.
├── __pycache__
│   └── t.cpython-37.pyc
├── graphics
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── formats
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   └── __init__.cpython-37.pyc
│   │   ├── jpg.py
│   │   └── png.py
│   └── primitive
│       ├── __init__.py
│       ├── __pycache__
│       │   └── __init__.cpython-37.pyc
│       ├── fill.py
│       ├── line.py
│       └── text.py
└── t.py

 

In [4]: import t                                                                                   
I am t

 After import module directly outputs the contents inside, running the module has been described.

From here you can explain, in general, my Lord run the file should be built on the top floor, so, when running this file, it is possible to add the proper working conditions of this script into the Python environment.

Avoid some of the guide packet error later (possibly guide packet error absolute path).

 

 

In this case, I can pass top-level package, very easy to import module

With this, the latter are better able to understand the absolute path to import the module.

 

When we file a package of clips to import, in fact, it is imported __init__ file, the contents of each file will run inside.

from graphics.formats import jpg

 

I am graphics package
I am formats package

 When I import graphics package the following formats jpg module package below, these two packages inside __init__ all executed.

 

The __init__ file can easily get you through the package management package inside the module, just by a relative path from. Import xxx

The same level of the block will be imported within this package,

This package can import the late .xxx call this module package name.

 

The use of the module package is very rich, Pythoncookbook book describes in more detail, and he is in front of a simple read some, and so difficult to add later use.

Guess you like

Origin www.cnblogs.com/sidianok/p/12081866.html