0

I have a bat file called create_bundle.bat that has only 1 line in it:

browserify ./index.js > ./bundle.js

and if I activate this file, it creates a bundle file

I would like to execute this bat file every time I run my python 3.7 code. I have this code:

import subprocess
import pathlib
import os

def create_bundle():
    directory_path = pathlib.Path(__file__).parent.absolute()
    file_path = os.path.join(directory_path, "create_bundle.bat")

    with open(os.path.join(directory_path, "temp_log.txt"), "w") as f:
        subprocess.call([file_path], stdout=f, stderr=f)

I am getting this error:

(PythonEnv) C:\Program Files\JetBrains\PyCharm 2019.3.1\jbr\bin>browserify ./index.js  1>./bundle.js 
Error: Cannot find module 'C:\Program Files\JetBrains\PyCharm 2019.3.1\jbr\bin\index.js' from 'C:\Program Files\JetBrains\PyCharm 2019.3.1\jbr\bin'
    at C:\Users\localuser\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:55:21
    at load (C:\Users\localuser\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:69:43)
    at onex (C:\Users\localuser\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:92:31)
    at C:\Users\localuser\AppData\Roaming\npm\node_modules\browserify\node_modules\browser-resolve\node_modules\resolve\lib\async.js:22:47
    at FSReqCallback.oncomplete (fs.js:158:21)

What am I doing wrong?

Kenny Smith
  • 729
  • 3
  • 9
  • 23
  • Does this answer your question? https://stackoverflow.com/questions/5469301/run-a-bat-file-using-python-code – WiLL_K Sep 15 '20 at 07:44
  • not really. I wrote this code: p = Popen("create_bundle.bat", cwd=directory_path) stdout, stderr = p.communicate() But I am getting this error: FileNotFoundError: [WinError 2] The system cannot find the file specified – Kenny Smith Sep 15 '20 at 07:52
  • When I execute: p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE), I get the same error from my original post – Kenny Smith Sep 15 '20 at 07:58
  • Are you sure the file path is correct? Add a `breakpoint()` before the subprocess call, and examine the `file_path`. – WiLL_K Sep 15 '20 at 08:00
  • The path is correct. For some reason, It looks like it searches for index .js file from 'C:\Program Files\JetBrains\PyCharm 2019.3.1\jbr\bin\index.js' and not my current folder – Kenny Smith Sep 15 '20 at 08:02
  • Oh I see the problem now. Please change your batch file from `browserify ./index.js > ./bundle.js` to `browserify index.js > bundle.js`. If you want the `bundle.js` to be in the same folder as well. – WiLL_K Sep 15 '20 at 08:10
  • Did it solve your problem? – WiLL_K Sep 15 '20 at 08:18
  • No. Still, the same error from my initial post – Kenny Smith Sep 15 '20 at 08:26
  • One last thing you could do it give the absolute path in you batch file. – WiLL_K Sep 15 '20 at 08:36
  • I give absolute path. directory_path = pathlib.Path(__file__).parent.absolute() gets the absolute path – Kenny Smith Sep 15 '20 at 08:40

0 Answers0