Python3基礎 sys.path 查看與修改導包的路徑


  •        Python : 3.7.3
  •          OS : Ubuntu 18.04.2 LTS
  •         IDE : pycharm-community-2019.1.3
  •       Conda : 4.7.5
  •    typesetting : Markdown

code

coder@ubuntu:~$ source activate py37
(py37) coder@ubuntu:~$ ipython
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys                                                              

In [2]: sys.path                                                                
Out[2]: 
['/home/coder/anaconda3/envs/py37/bin',
 '/home/coder/anaconda3/envs/py37/lib/python37.zip',
 '/home/coder/anaconda3/envs/py37/lib/python3.7',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/lib-dynload',
 '',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages/IPython/extensions',
 '/home/coder/.ipython']

In [3]: # 導包的時候,按照sys.path列表中的路徑進行按順序進行搜索                                 

In [4]: # 如果在sys.path[0]的路徑下找到了,那么就停止搜索,不會去尋找sys.path[1]                 

In [5]: type(sys.path)                                                                           
Out[5]: list

In [6]: sys.path.append("/home/coder")                                                           

In [7]: sys.path                                                                                 
Out[7]: 
['/home/coder/anaconda3/envs/py37/bin',
 '/home/coder/anaconda3/envs/py37/lib/python37.zip',
 '/home/coder/anaconda3/envs/py37/lib/python3.7',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/lib-dynload',
 '',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages/IPython/extensions',
 '/home/coder/.ipython',
 '/home/coder']

In [8]: # 這樣修改的話,是臨時的。再啟動時,恢復默認。                                           

In [9]: exit                                                                                     
(py37) coder@ubuntu:~$ ipython
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys                                                                               

In [2]: sys.path                                                                                 
Out[2]: 
['/home/coder/anaconda3/envs/py37/bin',
 '/home/coder/anaconda3/envs/py37/lib/python37.zip',
 '/home/coder/anaconda3/envs/py37/lib/python3.7',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/lib-dynload',
 '',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages',
 '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages/IPython/extensions',
 '/home/coder/.ipython']

In [3]: exit                                                                                     
(py37) coder@ubuntu:~$ conda deactivate
coder@ubuntu:~$ 

source_code

path = [
    '/home/coder/Downloads/pycharm-community-2019.1.3/helpers',
    '/home/coder/anaconda3/envs/py37/lib/python37.zip',
    '/home/coder/anaconda3/envs/py37/lib/python3.7',
    '/home/coder/anaconda3/envs/py37/lib/python3.7/lib-dynload',
    '/home/coder/anaconda3/envs/py37/lib/python3.7/site-packages',
]

resource

  • [文檔 - English] docs.python.org/3
  • [文檔 - 中文] docs.python.org/zh-cn/3
  • [規范] www.python.org/dev/peps/pep-0008
  • [規范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源碼] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平台] www.cnblogs.com
  • [平台] gitee.com


Python具有開源、跨平台、解釋型、交互式等特性,值得學習。
Python的設計哲學:優雅,明確,簡單。提倡用一種方法,最好是只有一種方法來做一件事。
代碼的書寫要遵守規范,這樣有助於溝通和理解。
每種語言都有獨特的思想,初學者需要轉變思維、踏實踐行、堅持積累。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM