正确的方式在Python 2.7中进行相对导入(Proper way to relative import in Python 2.7)

编程入门 行业动态 更新时间:2024-10-16 16:26:26
正确的方式在Python 2.7中进行相对导入(Proper way to relative import in Python 2.7)

我有一个Flask,我正在从Python3迁移到Python2.7 。

我的项目结构如下:

app/ model/ __init__.py exercise.py utils/ __init__.py loader.py

exercise.py类包含类Exercise 。 我需要在loader.py文件中使用它。

app文件夹本身位于Heroku项目的顶层,该项目有一个run.py文件,该文件从loader.py调用Loader类。

以前我有:

from app.model.exercise import Exercise

但这在Python2.7中不起作用。

追加到sys.path似乎也不起作用。

什么是正确的方法来做到这一点?

I have a Flask I am migrating from Python3 to Python2.7.

My project structure is as follows:

app/ model/ __init__.py exercise.py utils/ __init__.py loader.py

The exercise.py class contains the class Exercise. I need to use this in the loader.py file.

The app folder itself is at the top level of a Heroku project, which has a run.py file, which calls the Loader class from loader.py.

Previously I had:

from app.model.exercise import Exercise

but this does not work in Python2.7.

Appending to sys.path does not seem to work either.

What is the correct way to do this?

最满意答案

除非包含__init__.py否则文件夹不是Python包。 因此from app除非app文件夹包含__init__.py文件,否则无法from app导入。

A folder isn't a Python package unless it contains an __init__.py. So you can't import from app unless the app folder contains an __init__.py file.

更多推荐

本文发布于:2023-04-29 04:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   方式   Python   relative   Proper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!