简体   繁体   English

从相关目录导入用户创建的Python模块和数据文件

[英]Importing user-created Python modules and data files from relative directories

I'm building a set of Python modules that depend on each other. 我正在构建一组彼此依赖的Python模块。

My file directory currently looks like: 我的文件目录目前看起来像:

.
├── utilities
│   ├── __init__.py
│   ├── utility.py
│   ├── data.csv
├── src
│   ├── __init__.py
│   |── functions
│       ├── __init__.py
│       └── function.py
└── __init__.py

Further, function.py imports data from utilities/data.csv . 此外, function.pyutilities/data.csv导入数据。

From the top-level directory ( . ), I run python3 src/functions/function.py . 从顶级目录( . ),我运行python3 src/functions/function.py

And I receive the following import error: 我收到以下导入错误:

Traceback (most recent call last):
  File "src/functions/function.py", line 1, in <module>
    from utilities.utility import UtilityFunctionA
ImportError: No module named 'utilities'

How do I properly import utilities from the function.py file? 如何从function.py文件中正确导入utilities Or should I not be running nested files in the first place, and instead running files at the top-level of the directory? 或者我应该首先不运行嵌套文件,而是在目录的顶层运行文件?

The imports are successful when running this code from within PyCharm. 从PyCharm中运行此代码时导入成功。

Silly question, but I've been unable to figure it out despite reading a lot of documentation (and Googling). 愚蠢的问题,但尽管阅读了大量的文档(和谷歌搜索),我仍然无法弄明白。


UPDATE: 更新:

Using python3 -m src.functions.function works to run the module from the command line with proper imports and with successfully loading the csv. 使用python3 -m src.functions.function可以从命令行运行模块并使用正确的导入并成功加载csv。

However, when I then run the module from within PyCharm, for instance using 但是,当我从PyCharm中运行模块时,例如使用

在此输入图像描述

Now I receive the error that OSError: File b'utilities/data.csv' does not exist 现在我收到OSError: File b'utilities/data.csv' does not exist

Is there any way to setup my module to run both from within PyCharm and also from the command line? 有没有办法设置我的模块从PyCharm和命令行运行?

If you want to be able to do - from utilities.utility import UtilityFunctionA - from within function.py , when running from the top level directory - . 如果你希望能够做到 - from utilities.utility import UtilityFunctionA - 从function.py ,从顶级目录运行时 - . . You need to run the python file as a module , using -m option. 您需要使用-m选项将python文件作为模块运行。 Example - 示例 -

python3 -m src.functions.function

But The above method only works if you always run from the . 但是上述方法只有在你总是运行时才有效. directory (top-level directory) . 目录(顶级目录)。 I always either add the top-level directory manually into the PYTHONPATH environment variable. 我总是手动将顶级目录添加到PYTHONPATH环境变量中。

Or use os.path and relative path to add it to sys.path programmatically using __file__ . 或者使用os.path和相对路径使用__file__以编程方式将其添加到sys.path Example - 示例 -

import sys
import os.path
path_to_top = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..'))
sys.path.append(path_to_top)

After this do the import. 在此之后进行导入。 If the directory structures would always remain the same, this would work even in other systems, without having to set any environment variable. 如果目录结构总是保持不变,那么即使在其他系统中也是如此,而不必设置任何环境变量。


As per the updated requirements - 根据更新的要求 -

Now I receive the error that OSError: File b'utilities/data.csv' does not exist 现在我收到OSError:File b'utilities / data.csv'不存在的错误

Is there any way to setup my module to run both from within PyCharm and also from the command line? 有没有办法设置我的模块从PyCharm和命令行运行?

For reading files, Python uses the current working directory as the start point , and all relative paths are resolved relative to the current working directory. 对于读取文件,Python使用当前工作目录作为起点,并且相对于当前工作目录解析所有相对路径。 It is never a good idea to rely on the current working directory to be a particular directory as we can run python scripts from anywhere using absolute path to the script. 依赖当前工作目录作为特定目录绝不是一个好主意,因为我们可以使用脚本的绝对路径从任何地方运行python脚本。 For loading files as well, we can use os.path and relative paths to create the absolute path to the file. 对于加载文件,我们可以使用os.path和相对路径来创建文件的绝对路径。 Example - 示例 -

import os.path
path_to_datacsv = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','..','utilities,'data.csv'))

This imports properly: 这导入正确:

python3 -m src.functions.function

Based on How to do relative imports in Python? 基于如何在Python中进行相对导入?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM