简体   繁体   English

如果我的脚本使用从其他文件导入的函数,如何让我的脚本从 Unix 上的可执行 python 文件运行? (ModuleNotFoundError)

[英]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.

相关问题 Python 文件无法识别来自其他 Python 文件的导入函数 - Python file does not recognize imported functions from other Python files 如何从文件中运行python脚本? (UNIX) - How do I run a python script from a file? (UNIX) 如何在不使用计算机路径的情况下运行使用其他文件夹和文件的 python 文件? - How can I run a python file that uses other folders with files, without using the computer's path? 如何限制可以从导入模块调用的函数? 我可以将功能设为私有吗? (蟒蛇) - How can I restrict which functions are callable from an imported module? Can I make a function private? (Python) 如何从不同的脚本在 python 中运行 3 个函数? - How can I run 3 functions in python from a different script? 如何从 Windows 中的 Python 脚本创建可在 Mac 上运行的可执行文件? - How to create an executable from a Python script in Windows that can run on a Mac? 为什么我不能从我的 Python 脚本创建可执行文件? - Why can't I create an executable from my Python script? 如何使用相同的I / O从python脚本运行bash可执行文件? - How can I run a bash executable from a python script with the same i/o? 如何装饰从文件导入的所有函数? - How can I decorate all functions imported from a file? 如何从Python脚本调用可执行文件? - How to make a call to an executable from Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM