zoukankan      html  css  js  c++  java
  • 1. python跨目录调用模块

    快速镜像安装第三方库 :  pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy (三方库名字)        

    同目录下,我们可以直接调用模块,但是不同目录下调用模块却有些许不同。

    假设我们的目录结构如下,我们需要在test.py中调用calultater.py:

     一. 通过绝对路径调用(不太推荐)

          1. 获取calulator.py的绝对路径

          2. 通过import sys 将路径添加上

    具体:

    import sys

    sys.path.append(r'D:\05_test_software\project2\module')    #添加绝对路径

    from calulator import *   #*是对应的函数

    二. 通过相对路径调用 (推荐用法)

    import sys
    from os.path import dirname,abspath
    
    project_path = dirname(dirname(abspath(__file__)))
    #__file__用于获取文件的路径,abspath(__file__)获得绝对路径;
    #dirname()用于获取上级目录,两个dirname()相当于获取了当前文件的上级的上级即示例中project2
    sys.path.append(project_path+r'\module')
    #路径拼接成D:\05_test_software\mudule
    
    from calulater import *
    

      

  • 相关阅读:
    Tiny64140之初始化时钟
    Tiny6410之控制icache驱动
    Tiny6410之按键裸机驱动
    Linux -- man 、info、 whatis、 -h
    Linux -- which whereis
    Linux -- sudoers (简单:转)
    Linux -- sudo
    Linux -- sudoers文件
    Linux -- cp
    Linux -- mv
  • 原文地址:https://www.cnblogs.com/lintest/p/11697409.html
Copyright © 2011-2022 走看看