从bash脚本执行Python程序时出现ModuleNotFoundError [英] ModuleNotFoundError when executing Python program from bash script

查看:309
本文介绍了从bash脚本执行Python程序时出现ModuleNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以使用inotify-tools监视目录.将新文件添加到目录后,脚本将调用python程序,并将路径和填充名称作为参数传递.

I have a script which monitors a directory using inotify-tools. When a new file is added to the directory, the script calls a python program and passes the path and fill name as arguments.

一切正常,除了我得到一个ModuleNotFoundError ...有问题的模块是boto3(适用于AWS S3).

It all works fine, except that I get a ModuleNotFoundError... the module in question is boto3 (for AWS S3).

python脚本在Pycharms中运行良好,所以我真的不知道是什么导致了错误.

The python script works fine in Pycharms, so I really do not know what is causing the error.

监控脚本:

source=$1
pythonscriptlocation=$2

inotifywait -m $source -e create -e moved_to |
while read path action file; do
    python $pythonscriptlocation $path $file
done

Python脚本示例:

Python script example:

import argparse
import boto3

parser = argparse.ArgumentParser();
parser.add_argument("source_path");
parser.add_argument("filename");
args = parser.parse_args();
print(args.source_path);
print(args.filename);

更新-解决方案

事实证明,ANACONDA已将环境变量添加到我的.bashrc文件的底部...结果,它弄乱了我的PATH.删除了该行,然后删除了另外一个问题:在监视脚本中,我正在调用python,但是应该一直在调用python3.

It turned out that ANACONDA had added an environment variable to the bottom of my .bashrc file... as a result, it screwed up my PATH. Deleted the line and then 1 more problem: in the monitor script, I was calling python, but should have been calling python3.

推荐答案

您如何启动此脚本?我猜您正在运行的用户环境在某种程度上与交互式环境有所不同.最可能的原因是您在这两个环境之间设置的 PYTHONPATH 环境变量有所不同-在运行Python代码之前先尝试打印出该变量,看看是否有所不同.

How are you launching this script? I'd guess that the user environment you're running in is different from the interactive environment in some way. The most likely cause is that you're setting the PYTHONPATH environment variable differently between these two environments -- try printing that out before running your Python code and seeing if it differs.

另一种可能性是,您在两个环境中设置的路径不同,甚至没有运行相同的Python版本.检查您的 PATH 环境变量,看看它们是否相同.

Another possibility is that you've got your path set differently in your two environments, and you're not even running the same Python version. Check your PATH environment variable and see if these are the same.

这些可能不同的一个原因是,脚本在其中起作用的交互式环境是否正在从.profile文件中获取这些或其他配置. .profile 仅由登录外壳程序读取,而其他外壳程序则不读取.将它们放在您的 .bashrc 中而不是 .profile 中.

One reason these could be different is if your interactive environment, where the script works, is grabbing these or other configurations from your .profile file. The .profile is only read by login shells, not other shells. Putting these in your .bashrc rather than .profile might make a difference.

(不用说,如果您在这两个上下文中以其他用户身份运行,则两个用户的配置中都必须存在相关的环境变量.)

(It should go without saying that if you're running as a different user in these two contexts, that the relevant environment variables need to be present in both users' configurations.)

这篇关于从bash脚本执行Python程序时出现ModuleNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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