zoukankan      html  css  js  c++  java
  • 17.python自定义模块的导入方式

    1.直接用import导入

     

     

     

    最后运行main.py可以看到命令行窗口输出了一句:你好,这样就完成了

     2.通过sys模块导入自定义模块的路径path

     

     

     3、在环境变量中找到自定义模块

    这个方法原理就是利用了系统变量,python会扫描path变量的路径来导入模块,可以在系统path里面添加。

    模块和执行文件目录结构跟上图一样:

    • 执行文件main.py在main目录下
    • pwcong模块在python目录下

    我们创建一个 module_pwcong.pth 文件,里面内容就是 pwcong模块所在的目录:

    C:UsersPwcongDesktoppython

    将该 module_pwcong.pth 文件放到这里:
    python安装目录Python35Libsite-packages

    例如我的:

    然后 main.py 导入并使用自定义模块:

    # -*- coding: utf-8 -*-
    
    import pwcong
    
    pwcong.hi()

    最后执行 main.py 文件,可以输出 hi ,第三种方式完成。

  • 相关阅读:
    Valid Parentheses
    Remove Nth Node From End of List
    守护线程、守护进程
    4Sum
    Letter Combinations of a Phone Number
    3sum closest
    Excel Sheet Column Number
    Majority Element
    Balanced Binary Tree
    Maximum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/ubuntu1987/p/11855467.html
Copyright © 2011-2022 走看看