导入位于当前文件夹的同级文件中的库 [英] Import a library which is in a sibling of the current folder

查看:167
本文介绍了导入位于当前文件夹的同级文件中的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有文件夹结构

  lib / 
abcd /
__init.py__
lib.py
app.py

代码


$来自lib.abcd的b $ b

 导入lib 

作品。但是具有以下文件结构:

  bin / 
app.py
lib /
abcd /
__init.py__
lib.py

代码

来自..lib.abcd的

 进口lib 

给出了导入错误。



如何正确地执行导入库是否在当前文件夹的同级文件中?(或同级文件夹的子文件夹)



我知道可能会有一些涉及添加 lib / 到PATH,但是是否有一个优雅的Python解决方案?



如果没有,是否有真正的内部原因来阻止用户以简单的方式执行此简单导入?

解决方案

执行此操作的方法


< hr />

方法1:使用sys模块:您可以使用 sys 模块。要导入 lib 软件包,可以使用下面列出的两个代码之一:

  import sys 
sys.path.append('< PATH_TO_LIB_FOLDER>')

from lib.abcd import lib

  import sys 
sys .path.insert(0,'< PATH_TO_LIB_FOLDER>')




方法2:使用os模块:另一种方法是使用 os 模块。这是一个示例代码,通过调用 os使用 os 模块导入 lib 模块.path.join 方法:

  import os 

路径= os.path.join(< PATH> / lib / abcd, lib)
从lib.abcd导入lib




方法3:将模块添加到PYTHONPATH:在大多数情况下,这不是最好的方法,但是如果您不这样做,不想继续使用 sys os 模块导入 lib ,这是理想的。您所需要做的就是在bash终端中键入以下内容:

  export PYTHONPATH =< PATH_TO_LIB> python lib.py 

然后在您的python shell中,您可以像这样导入它:


< pre class = lang-py prettyprint-override> 从lib.abcd导入lib




方法4:组合sys和os模块(推荐)::这是最有效的方法,可以节省很多时间。这段代码结合了 os sys 模块,如下所示:

  import sys,os 
sys.path.append(os.path.abspath(os.path.join('..','lib'))) )

然后,您可以像这样轻松地导入模块:

 从lib.abcd导入lib 




所有代码的工作方式:


以上所有代码非常简单。除了方法#3 以外的所有示例,都将模块暂时添加到 PYTHONPATH 中。另一方面,方法#3 将模块永久添加到 PYTHONPATH 中。


With the folder structure

lib/
    abcd/
        __init.py__
        lib.py
app.py

the code

from lib.abcd import lib

works. But with this file structure:

bin/
    app.py
lib/
    abcd/
        __init.py__
        lib.py

the code

from ..lib.abcd import lib     

gives an import error.

How to do the import properly when the library is in a sibling of the current folder? (or subfolder of a sibling folder)

I know that there might some hack that involves adding lib/ to the PATH, but is there an elegant Pythonic solution?

If not, is there a real internal reason to prevent users to do this simple import in a simple way?

解决方案

Methods to do this


Method #1: Using the sys module: You can easily accomplish what you are trying to do using the sys module. To import the lib package, you can use one of the two codes listed below:

import sys
sys.path.append('<PATH_TO_LIB_FOLDER>')

from lib.abcd import lib

or

import sys
sys.path.insert(0, '<PATH_TO_LIB_FOLDER>')


Method #2: Using the os module: Another method is to use the os module. Here is an example code that imports the lib module using the os module by invoking the os.path.join method:

import os

path = os.path.join("<PATH>/lib/abcd", "lib")
from lib.abcd import lib


Method #3: Add the module to your PYTHONPATH: This is not the best method in most cases, but if you don't want to keep using the sys or os module to import lib, this is ideal. All you have to do is type this in your bash terminal:

export PYTHONPATH=<PATH_TO_LIB> python lib.py

Then in your python shell you can import it like this:

from lib.abcd import lib


Method #4: Combine the sys and os module (recommended): This is the most efficient method and will save you a lot of time. This code combines the os and sys module like this:

import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'lib')))

Then you can import your module with ease like this:

from lib.abcd import lib


How all the codes work:

All the codes above are very simple. All the examples except for "Method #3", add your module to the PYTHONPATH temporarily. "Method #3" on the other hand, adds the module to your PYTHONPATH permanently.

这篇关于导入位于当前文件夹的同级文件中的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆