1. 程式人生 > >Python3基礎 sys.path.append 增加模塊的搜索路徑

Python3基礎 sys.path.append 增加模塊的搜索路徑

連接 block 一件事 naconda cheng google 自帶 com author

?

  • ???? Python : 3.7.0
  • ?????? OS : Ubuntu 18.04.1 LTS
  • ?????? IDE : PyCharm 2018.2.4
  • ????? Conda : 4.5.11
  • ???typesetting : Markdown

?

code

"""
@Author : 行初心
@Date   : 18-10-2
@Blog   : www.cnblogs.com/xingchuxin
@Gitee  : gitee.com/zhichengjiu
"""
import sys
import os


def main():
    path = os.path.abspath(os.curdir)
    print(path)
    path = str(path)

    # 用+連接字符串,好用 。但是連接大量的字符串就不好了。要用自帶的函數
    new_path = path + ‘/MyModuleFile‘
    print(new_path)

    print("舊", sys.path)

    # 將MyModuleFile(跟主代碼在一起的文件夾)添加到搜索路徑當中
    sys.path.append(new_path)

    print("新", sys.path)
    # 也可以進行優化,刪除不必要的顯示後成為一個函數。方便以後調用


if __name__ == ‘__main__‘:
    main()

?

result

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/base/demo.py
/home/coder/PycharmProjects/base
/home/coder/PycharmProjects/base/MyModuleFile
舊 [‘/home/coder/PycharmProjects/base‘, ‘/home/coder/PycharmProjects/base‘, ‘/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/pycharm-2018.2.4/helpers/pycharm_matplotlib_backend‘]
新 [‘/home/coder/PycharmProjects/base‘, ‘/home/coder/PycharmProjects/base‘, ‘/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/pycharm-2018.2.4/helpers/pycharm_matplotlib_backend‘, ‘/home/coder/PycharmProjects/base/MyModuleFile‘]

Process finished with exit code 0

?

resource

  • [文檔] docs.python.org/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的設計哲學:優雅,明確,簡單。提倡用一種方法,最好是只有一種方法來做一件事。
代碼的書寫要遵守規範,這樣有助於溝通和理解。
每種語言都有獨特的思想,初學者需要轉變思維、踏實踐行、堅持積累。

Python3基礎 sys.path.append 增加模塊的搜索路徑