问题
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.
It all works fine, except that I get a ModuleNotFoundError
... the module in question is boto3 (for AWS S3).
The python script works fine in Pycharms, so I really do not know what is causing the error.
Monitor script:
source=$1
pythonscriptlocation=$2
inotifywait -m $source -e create -e moved_to |
while read path action file; do
python $pythonscriptlocation $path $file
done
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);
Update - Solution
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
.
回答1:
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.
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.
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.)
来源:https://stackoverflow.com/questions/42563446/modulenotfounderror-when-executing-python-program-from-bash-script