[英]Run python script inside bash file in Windows
I was tasked with running a script developed by someone else.我的任务是运行由其他人开发的脚本。 It's quite simple, but it's a bash script and I had almost never touched Linux, so I'm not sure on how to proceed.
这很简单,但它是一个 bash 脚本,我几乎从未接触过 Linux,所以我不确定如何进行。 I was able to install WLS so I can run
bash
on Windows, but now I have to run a specific python script inside the bash script.我能够安装 WLS,所以我可以在
bash
上运行 bash,但现在我必须在 ZD5164BFC6B7EE9A3B755DDZ 脚本中运行特定的 python 脚本。 The script goes like this:脚本是这样的:
#!/bin/bash
BASE_DIR=dir
find $BASE_DIR -type f | grep '\.pdf' | while read pdf_filename; do
filebase=`echo $pdf_filename | cut -d '.' -f 1`
txt_filename="$filebase.txt"
echo "Processing $pdf_filename..."
pdf2txt.py $pdf_filename > $txt_filename
echo "Done!"
done
It should run the pdf2txt.py
script, but I'm getting this error:它应该运行
pdf2txt.py
脚本,但我收到此错误:
convert_all.sh: line 8: pdf2txt.py: command not found
So, I'm not sure how to connect bash to my Python installation, I'm guessing it's not being able to find it.所以,我不确定如何将 bash 连接到我的 Python 安装,我猜它找不到它。 I would ideally like to link it to this project's virtual environment.
理想情况下,我希望将其链接到该项目的虚拟环境。 Any ideas on how to proceed?
关于如何进行的任何想法?
Edit:编辑:
This is my current error based on what I responded to @DV82XL:根据我对@DV82XL 的回复,这是我当前的错误:
/mnt/c/Users/jeco_/Desktop/Otros repositorios/sesgo_medios/Code/hello.py: line 1: $'\r': command not found
/mnt/c/Users/jeco_/Desktop/Otros repositorios/sesgo_medios/Code/hello.py: line 2: syntax error near unexpected token `"hello world"'
/mnt/c/Users/jeco_/Desktop/Otros repositorios/sesgo_medios/Code/hello.py: line 2: `print("hello world")'
Can you convert the bash script to Python?能否将 bash 脚本转换为 Python? That way you can easily run in Windows or Linux without WSL.
这样,您可以在没有 WSL 的情况下轻松地在 Windows 或 Linux 中运行。
If you must run the bash script in WSL, make sure Python is installed in WSL:如果必须在 WSL 中运行 bash 脚本,请确保在 WSL 中安装了 Python:
type -a python
or type -a python3
. type -a python
或type -a python3
。 This will give you the interpreter path. If it doesn't show up, you will need to install Python on WSL:如果没有出现,则需要在 WSL 上安装 Python:
sudo apt update && upgrade
sudo apt install python3 python3-pip ipython3
Then do these things:然后做这些事情:
echo $PATH
.echo $PATH
确保 Python 解释器位于 PATH 环境变量中。 If it's not there, add it by typing export PATH="$PATH:/usr/bin/python3"
or add it to ~/.profile.export PATH="$PATH:/usr/bin/python3"
添加它或将其添加到 ~/.profile。 On Linux, it's usually included by default.python --version
or python3 --version
to get versions and make sure python path was set correctly.python --version
或python3 --version
以获取版本并确保 python 路径设置正确。#!/path/to/interpreter
#!/usr/bin/python3
#!/usr/bin/python3
#./usr/bin/python2.7
#./usr/bin/python2.7
chmod +x pdf2txt.py
chmod +x pdf2txt.py
Now you should be able to run pdf2txt.py
directly instead of python pdf2txt.py
.现在您应该能够直接运行
pdf2txt.py
而不是python pdf2txt.py
。
Hint: In WSL, you can access your Windows files at /mnt/c/Users/<user>/path/to/file
if you need to.提示:在 WSL 中,如果需要,可以在
/mnt/c/Users/<user>/path/to/file
访问 Windows 文件。
If this doesn't work, please let us know which Linux distro/version you are running and what version of Python these scripts require.如果这不起作用,请告诉我们您正在运行哪个 Linux 发行版/版本以及这些脚本需要什么版本的 Python。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.