[英]How can I make my script run from executable python file on Unix if it uses functions imported from other files? (ModuleNotFoundError)
I made a script to validate and clean a complex DataFrame with calls to a database.我编写了一个脚本来验证和清理复杂的 DataFrame 并调用数据库。 I divided the script into modules.
我将脚本分成模块。 I import them frequently between files to use certain functions and variables that are spread across the different files and directories.
我经常在文件之间导入它们以使用分布在不同文件和目录中的某些函数和变量。
Please see image to see how files are distributed across directories:请查看图片以了解文件如何跨目录分布:
Directory structure目录结构
An example would look like this:一个示例如下所示:
# Module to clean addresses: address_cleaner.py
def address_cleaner(dataframe):
...
return dataframe
# Pipeline to run all cleaning functions in order looks like: pipeline.py
from file1 import function1
...
def pipeline():
df = function1
...function2(df)
...function3(df)
...
return None
# Executable file where I request environment variables and run pipeline: exe.py
from pipeline import pipeline
import os
...
pipeline()
...
When I run this on Unix:当我在 Unix 上运行它时:
% cd myproject
% python executable.py
This is one of the import cases, which I import to avoid hardcoding environment variable string names:这是导入案例之一,我将其导入以避免硬编码环境变量字符串名称:
File "path/to/executable.py", line 1, in <module>
from environment_constants import SSH_USERNAME, SSH_PASSWORD, PROD_USERNAME, PROD_PASSWORD
ModuleNotFoundError: No module named 'environment_constants
I get a ModuleNotFoundError when I run executable.py on Unix, that calls the pipeline shown above and it seems as if all the imports that I did between files to use a function, variable or constant from them, especially those in different directories didn't reach to each other.当我在 Unix 上运行 executable.py 时,我得到一个 ModuleNotFoundError,它调用上面显示的管道,似乎我在文件之间执行的所有导入都使用 function,来自它们的变量或常量,尤其是不同目录中的那些没有t 到达对方。 These directories all belong to the same parent directory "CD-cleaner".
这些目录都属于同一个父目录“CD-cleaner”。
Is there a way to make these files read from each other even if they are in different folders of the script?有没有办法让这些文件相互读取,即使它们位于脚本的不同文件夹中?
Thanks in advance.提前致谢。
Either create proper python modules that you can install and manage with pip or (easier) just always use your root as your working directory.要么创建适当的 python 模块,您可以使用 pip 安装和管理,或者(更简单)始终使用您的根目录作为您的工作目录。 Set the import paths accordingly and then just run the file from the root folder such as
python generate/generate_dataframe.py
when you are in the joor-cd-cleaner
directory.相应地设置导入路径,然后在
joor-cd-cleaner
目录中运行根文件夹中的文件,例如python generate/generate_dataframe.py
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.