Python:没有包或模块的相对导入 [英] Python: relative imports without packages or modules

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

问题描述

在尝试处理相对导入并阅读了许多关于它的 StackOverflow 帖子后,我开始意识到这比它需要的要复杂得多.

After trying to deal with relative imports and reading the many StackOverflow posts about it, I'm starting to realize this is way more complicated than it needs to be.

除了我,没有其他人在使用此代码.我不是在创建工具或 api,所以我认为没有必要创建包"或模块".我只想将我的代码组织到文件夹中,而不是在同一目录中包含 50 个脚本.这太荒谬了.

No one else is using this code but me. I'm not creating a tool or an api, so I see no need to create a "package" or a "module". I just want to organize my code into folders, as opposed to having a 50 scripts all in the same directory. It's just ridiculous.

基本上,只想拥有三个文件夹并且能够从我想要的任何地方导入脚本.一个文件夹,其中包含一些具有实用功能的脚本.另一个文件夹包含大部分代码,另一个文件夹包含一些实验(我正在做机器学习研究).

Basically, just want to have three folders and be able to import scripts from wherever I want. One folder that contains some scripts with utility functions. Another folder has the majority of the code, and another folder that contains some experiments (I'm doing machine learning research).

/project/code/
/project/utils/
/project/experiments/

我需要做的就是在文件夹之间导入 python 文件.

All I need to do is import python files between folders.

我发现的一个解决方案是将 __init__.py 文件放在每个目录中,包括文件夹所在的根目录.但是我需要在该目录的父目录中运行我的实验.

One solution I have found is putting __init__.py files in each directory including the root directory where the folders live. But I then need to run my experiments in the parent of that directory.

/experiment1.py
/experiment2.py
/project/__init__.py
/project/code/__init__.py
/project/utils/__init__.py

所以上面的工作.但是我有两个问题.我的实验与代码不在同一个文件夹中.这很烦人,但我想我可以忍受它.但更大的问题是我不能把我的实验放在不同的文件夹中:

So the above works. But then I have two problems. My experiments don't live in the same folder as the code. This is just annoying, but I guess I can live with it. But the bigger problem is that I can't put my experiments different folders:

/experiments/experiment1/something.py
/experiments/experiment2/something_else.py
/project/__init__.py
/project/code/__init__.py
/project/utils/__init__.py

我想我可以将项目目录符号链接到每个实验文件夹中,但这太荒谬了.

I suppose I could symlink the project directory into each experiment folder, but thats ridiculous.

另一种方法是将所有内容都视为模块:

The other way of doing it is treating everything like a module:

/project/__init__.py
/project/code/__init__.py
/project/utils/__init__.py
/project/experiments/__init__.py
/project/experiments/experiment1/something.py
/project/experiments/experiment2/something_else.py

但是我必须用 python -m project.experiments.experiment1.something 运行我的实验,这对我来说似乎很奇怪.

But then I have to run my experiments with python -m project.experiments.experiment1.something which just seems odd to me.

我目前找到的解决方案是:

A solution I have found thus far is:

import imp
import os
currentDir = os.path.dirname(__file__)
filename = os.path.join(currentDir, '../../utils/helpful.py')
helpful = imp.load_source('helpful',filename)

这行得通,但它既乏味又丑陋.我尝试创建一个脚本来为我处理这个问题,但是 os.path.dirname(__file__) 是错误的.

This works, but it is tedious and ugly though. I tried creating a script to handle this for me, but then the os.path.dirname(__file__) is wrong.

肯定有人像我一样试图在文件夹中组织他们的 Python 脚本,而不是将它们全部放在一个目录中.

Surely someone has been in my position trying to organize their python scripts in folders rather than having them all flat in a directory.

这个问题有什么好的、简单的解决方案,还是我必须求助于上述方法之一?

Is there a good, simple solution to this problem, or will I have to resort to one of the above?

推荐答案

将 python 文件作为模块运行对我来说似乎也很奇怪.您可以通过将 main.py 文件放在根目录中,将您的实验仍然放在文件夹中.所以文件夹树如下所示;

Running python files as a module seems also odd to me. You can put your experiments in a folder still by putting a main.py file on the root directory. So the folder tree would be the following;

/project/experiments
/project/code
/project/utils
/project/main.py

在您的 main.py 文件上调用您的实验,并在 main.py 文件上进行导入.__init__.py 也应该在每个文件夹中.

Call your experiments on your main.py file and make your imports on main.py file. The __init__.pys should also be inside each folder.

通过这种方式,您不需要将 py 文件作为 python 模块运行.此外,该项目将只有一个入口点,这在许多情况下非常有用.

By this way, you won't need to run the py files as a python module. Also, the project will have a single entry point which is very useful in many cases.

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

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