python相对导入 [英] python relative import

查看:72
本文介绍了python相对导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中进行相对导入.但是我听不懂语法,因此每次在SO中搜索它时,我都找不到答案:

I'm trying to make a relative import in python. but I can't understand the syntax and everytime I search for it here in SO, I can't get an answer:

这是我的文件夹结构:

Root
    libraries
        mylibrary
        __init__
    projects
        project
            myproject.py

我想使用相对路径导入"mylibrary",这样做的语法是什么?

and I want to import 'mylibrary' using a relative path, what's the syntax for doing it?

推荐答案

您必须将目录添加到python路径.

You have to add the directory to your python path.

import sys
sys.path.append("/libraries") 

但是我认为最好将这些库放在需要它的项目的文件夹中,或者将它们安装到sys.path中已经存在的标准位置之一.

But I think it might be better to either put the libraries in the folders of the projects that need it or just install them to one of the standard places already in sys.path.

这篇关于python相对导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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