developer tip

쉘 스크립트 : 쉘 스크립트 내에서 파이썬 프로그램 실행

copycodes 2020. 8. 14. 07:54
반응형

쉘 스크립트 : 쉘 스크립트 내에서 파이썬 프로그램 실행


나는 대답을 인터넷 검색을 시도했지만 운이 없었습니다.

작업용 슈퍼 컴퓨터 서버를 사용해야하지만 Python 스크립트를 실행하려면 쉘 스크립트를 통해 실행해야합니다.

예를 들어 job.sh실행 하고 싶습니다.python_script.py

어떻게 이룰 수 있습니까?


Python 실행 파일이 PATH 환경 변수에 있는지 확인한 다음 스크립트에 추가하십시오.

python path/to/the/python_script.py

세부:

  • job.sh 파일에 다음을 넣으십시오.
#!/bin/sh
python python_script.py
  • 다음 명령을 실행하여 스크립트를 실행할 수 있도록합니다. chmod u+x job.sh
  • 그것을 실행하십시오 : ./job.sh

방법 1-셸 스크립트 만들기 :

당신이 파이썬 파일이 있다고 가정 hello.py 라는 파일을 생성 job.sh하는 포함

#!/bin/bash
python hello.py

다음을 사용하여 실행 가능 표시

$ chmod +x job.sh

그런 다음 실행

$ ./job.sh

방법 2 (더 나은)-파이썬 자체가 쉘에서 실행되도록합니다.

스크립트를 수정 hello.py하고 이것을 첫 번째 줄로 추가하십시오.

#!/usr/bin/env python

다음을 사용하여 실행 가능 표시

$ chmod +x hello.py

그런 다음 실행

$ ./hello.py

임호, 글쓰기

python /path/to/script.py

특히 요즘에는 아주 잘못되었습니다. 어떤 파이썬? python2.6? 2.7? 3.0? 3.1? 대부분의 경우 파이썬 파일의 shebang 태그에 파이썬 버전을 지정해야합니다. 나는 사용하는 것이 좋습니다

#! / usr / bin / env python2 # 또는 python2.6 또는 python3 또는 심지어 python3.1
호환성을 위해.

이 경우 스크립트를 실행하여 직접 호출하는 것이 훨씬 좋습니다.

#! / bin / bash

/path/to/script.py

이렇게하면 필요한 파이썬 버전이 하나의 파일에만 작성됩니다. 요즘 대부분의 시스템은 그동안 python2와 python3을 가지고 있으며, symlink pythonpython3 을 가리키는 반면 대부분의 사람들은 python2를 가리키는 것으로 예상합니다 .


python scriptname.py로 호출 할 수 있어야합니다.

예 :

# !/bin/bash

python /home/user/scriptname.py 

또한 스크립트에 실행할 권한이 있는지 확인하십시오.

chmod u + x scriptname.py를 사용하여 실행 가능하게 만들 수 있습니다.

편집 : 구타 :-p


이것은 나를 위해 가장 잘 작동합니다. 스크립트 맨 위에 다음을 추가하십시오.

#!c:/Python27/python.exe

(C : \ Python27 \ python.exe는 내 컴퓨터에있는 python.exe의 경로입니다.) 그런 다음 다음을 통해 스크립트를 실행합니다.

chmod +x script-name.py && script-name.py

이것은 나를 위해 작동합니다.

  1. 새 셸 파일 작업을 만듭니다. 예를 들어, 다음 touch job.sh과 같이 명령을 추가하여 python 스크립트를 실행합니다 (해당 파이썬에 명령 줄 인수를 추가 할 수도 있습니다. 저는 보통 명령 줄 인수를 미리 정의합니다).

    chmod +x job.sh

  2. 내부 job.sh에 다음 py 파일을 추가하십시오.

    python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "Done with python_file.py"

    python_file1.py argument1 argument2 argument3 >> testpy-output.txt && echo "Done with python_file1.py"

job.sh의 출력은 다음과 같습니다.

Done with python_file.py

Done with python_file1.py

나는 일반적으로 미리 정의 된 다른 인수로 여러 파이썬 파일을 실행해야 할 때 이것을 사용합니다.

참고 : 여기서 무슨 일이 일어나고 있는지 간단히 알려드립니다.

python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "completed with python_file.py" . 

  • Here shell script will run the file python_file.py and add multiple command-line arguments at run time to the python file.
  • This does not necessarily means, you have to pass command line arguments as well.
  • You can just use it like: python python_file.py, plain and simple. Next up, the >> will print and store the output of this .py file in the testpy-output.txt file.
  • && is a logical operator that will run only after the above is executed successfully and as an optional echo "completed with python_file.py" will be echoed on to your cli/terminal at run time.

Save the following program as print.py:

#!/usr/bin/python3
print('Hello World')

Then in the terminal type:

chmod +x print.py
./print.py

I use this and it works fine

#/bin/bash
/usr/bin/python python python_script.py

Since the other posts say everything (and I stumbled upon this post while looking for the following).
Here is a way how to execute a python script from another python script:

Python 2:

execfile("somefile.py", global_vars, local_vars)

Python 3:

with open("somefile.py") as f:
    code = compile(f.read(), "somefile.py", 'exec')
    exec(code, global_vars, local_vars)

and you can supply args by providing some other sys.argv

참고URL : https://stackoverflow.com/questions/4377109/shell-script-execute-a-python-program-from-within-a-shell-script

반응형