1

I have the following Go code executing an external Python script.

package main

import (
    "log"
    "os"
    "os/exec"
    "fmt"
)
func main(){

  //Call Python script
  cmd := exec.Command(`C:\Python35\python35.exe`, `C:\...\py_file.py`)
  cmd.Stdout = os.Stdout
  cmd.Stderr = os.Stderr
  log.Println(cmd.Run())

}

It causes this error within the Py file:

ImportError: No module named 'youtube_transcript_api'

However, if I run the Py file by itself, it works perfectly fine. It should output JSON.

Am I missing something? Let me know if you need more info!

Thank you so much, M2com

2
  • are you using python venv's and could it be your go code does not use a venv Commented Apr 24, 2020 at 9:32
  • I don't think so? I am using Python 3.5 which I installed through the Windows Store. I'm pretty sure that it operates like regularly installed Python. Commented Apr 24, 2020 at 14:02

1 Answer 1

2

1)Windows

First of all make your python script executable anfInclude python interpreter in environment variable

eg:

cmd := exec.Command("script.py") //file path C:\...\py_file.py cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr log.Println(cmd.Run())

2)Linux

First of all do not forget to make your python script executable (permissions and #!/usr/local/bin/python at the beginning

eg:

cmd := exec.Command("bash", "-c", "python3 script.py")
_, err = cmd.Output()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply! I'm not really sure what you mean by making the Python executable for Windows. When I run the python3 command in CMD Prompt, it works. However, I have to specify where the python35 exe is in Go. Could you walk me through what you mean?
Making python executable mean giving executable permission to your script(by default it is executable to don't worry about). just add python35 path in your environment variable and this will work.
So I had both py27 and py35 installed on my computer. What I did was rename the exe in those folders so that py35 was named python.exe instead of python35.exe. For some reason that seemed to work! From there I was able to just use the 'python' command instead of use the full path. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.