简体   繁体   English

从 shell 脚本运行 python 文件时出现 ModuleNotFoundError 错误

[英]ModuleNotFoundError error when running a python file from shell script

I have a python file download.py in which I am importing a module opc我有一个 python 文件download.py我在其中导入一个模块opc

Now, this python file invoked from a shell script build.sh like:现在,这个 python 文件从 shell 脚本build.sh调用,如:

python3 $(pwd)/scripts/download.py -d $dir_name

When I am running the shell script, I am getting the following error:当我运行 shell 脚本时,我收到以下错误:

Traceback (most recent call last):
  File "/home/ray/Desktop/repo/image-builder/scripts/download.py", line 14, in <module>
    from opc.utils import file_utils, logger
ModuleNotFoundError: No module named 'opc'

This is how shell script is invoked:这是调用 shell 脚本的方式:

sudo sh scripts/src/build.sh

I have a virtual environment active where I have installed this package.我有一个活动的虚拟环境,我安装了这个 package。 And this package is there as well还有这个 package

>pip list

Package                           Version
--------------------------------- -------
attrs                             21.2.0
.
.
opc                               0.8.0

Interestingly, when I run the python code directly like有趣的是,当我直接运行 python 代码时

python3 scripts/download.py -d dir_path

it works fine.它工作正常。 I don't not get ModuleNotFoundError error.我没有收到ModuleNotFoundError错误。

Also, this works fine as well:此外,这也可以正常工作:

❯ python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import opc.utils.global_vars as g
>>> print(g.LOCAL_HOST)
127.0.0.1
>>>

Directory structure:目录结构:

/home/ray/Desktop/repo/image-builder/scripts/download.py
/home/ray/Desktop/repo/image-builder/scripts/src/build.sh

and i am running commands from:我正在运行以下命令:

/home/ray/Desktop/repo/image-builder

This is likely to be because when you invoke the shell script, the python path is different from when you're running the Python interpreter.这可能是因为当您调用 shell 脚本时,python 路径与您运行 Python 解释器时的路径不同。 If that is the case, there are several ways to address that.如果是这种情况,有几种方法可以解决这个问题。

What happens if you manually change into the same directory as download.py and run your python script from there?如果您手动更改到与download.py相同的目录并从那里运行 python 脚本会发生什么? Something like:就像是:

cd scripts
python download.py -d <some-dir>

Does that work, or do you still hit the same problem?这行得通,还是你仍然遇到同样的问题?

In shell scripts like this I first make sure the script will always be invoking the python app from the exact same place in the file system, so that I don't hit these python path issues.在像这样的 shell 脚本中,我首先确保脚本将始终从文件系统中完全相同的位置调用 python 应用程序,这样我就不会遇到这些 python 路径问题。 If you know for sure that python download.py -d <some-dir> works from inside the same directory as download.py , then you can add something like this to your shell script (assuming your shell script and download.py are in the same directory):如果您确定python download.py -d <some-dir>在与download.py相同的目录中工作,那么您可以将这样的内容添加到您的 shell 脚本中(假设您的 Z2591C98B70119FE624898B download.py同一目录):

pushd "${0%/*}" # this changes directory to the directory where the shell script itself lives

# do your stuff here - run your python app, etc.

popd # returns to whatever directory we were originally in

In this way, I can always invoke the shell script from whatever directory I am in and I know it will behave the same way - the current directory is no longer relevant.这样,我总是可以从我所在的任何目录调用 shell 脚本,并且我知道它的行为方式相同 - 当前目录不再相关。

I wouldn't use $(pwd) , because that will give the current directory, which varies, so your shell script will only ever work properly when invoked from inside the right directory if you do that.我不会使用$(pwd) ,因为这会给出当前目录,它会有所不同,所以如果你这样做,你的 shell 脚本只有在从正确的目录中调用时才能正常工作。

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

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