Python导入从上面的目录 [英] Python Imports From The Directory Above

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

问题描述

我有一个我的python应用程序的目录结构,其中在主文件夹中有一个名为handlers的文件夹,在该文件中,我有一个所有处理程序应该继承的base.py。我还有一个名为users的文件夹,其中包含与用户有关的所有处理程序。

  \main 
\handlers
base.py
__init__.py
\users
__init__.py
authenticated.py
logout.py

我的问题是无法在用户文件夹中的任何文件中导入基数,但可以从用户文件夹导入。我知道我必须做一些像bellow这样的事情。

  from ..handlers import * 

但是,由于某些原因我无法做到这一点,所以我想要做的是将base.py导入到users目录中的文件和我添加的其他目录稍后的日期。

解决方案

  import sys 
sys.path.append '/你/ dir /去/这里')
从基本的import foo

这应该允许您从您选择的任何目录导入东西。


I have a directory structure for my python application where in the main folder I have a folder called handlers and in that file I have a base.py that all handlers should inherit from . I also have a folder called users that contains all the handlers relating to users.

\main
      \handlers
      base.py
      __init__.py
      \users
                __init__.py
                authenticated.py
                logout.py   

My issue is I can't import the base in any file in the users folder but can import from the users folder. I know that I have to do something like bellow

from ..handlers import *    

But that does not work for some reason all I am trying to do is import base.py into files in the users directory and other directorys I add at a later date.

解决方案

import sys
sys.path.append('/your/dir/goes/here')
from base import foo

Something like that should permit you to import stuff from any directory of your choosing.

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

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