您的位置:首页 > 脚本大全 > > 正文

怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)

更多 时间:2022-03-28 01:43:33 类别:脚本大全 浏览量:911

怎么在当前目录调用python库

Python父目录、子目录的相互调用方法

最近在使用python的过程中经常遇到找不到该模块的问题。其中一个就是父目录子目录之间相互调用的情况。下面简单总结下。

怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)

我们在f:\code文件夹下面创建一个test文件夹

而test文件夹里面如下

怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)

包含两个子目录

怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)

a.py

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • def showdata():
  •   print("this is a")
  •  
  • def plus():
  •   a=1
  •   b=2
  •   print(a+b)
  • b.py

  • ?
  • 1
  • 2
  • def show():
  •   print("this is b")
  • 从父目路test.py调用a和b

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • from test1.a import showdata
  • from test1.a import plus
  • from test2.b import show
  •  
  • showdata()
  • show()
  • plus()
  • 怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)

    如何c.py想要调用另一个文件夹的a的话,需要加上sys.path.apend(“..”)

    c.py

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • import sys
  •  
  • sys.path.append("..")
  • from test1 import a as t
  •  
  • t.showdata()
  • 怎么在当前目录调用python库(Python父目录、子目录的相互调用方法)

    以上这篇python父目录、子目录的相互调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。

    原文链接:https://blog.csdn.net/gzj_1101/article/details/78555402