title: How can I execute JavaScript code from Python?
body: <p>Lets say that I have this code inside a JavaScript file:</p>
  
  <pre><code>var x = 10;
  x = 10 - 5;
  console.log(x);
  function greet() {
    console.log("Hello World!");
  }
  greet()
  </code></pre>
  
  <p>How would I use Python to execute this code and <em>"print"</em> <code>x</code> and <code>Hello World!</code>?<br />
  Here is some pseudo code that further explains what I'm thinking:</p>
  
  <pre><code># 1. open the script
  script = open("/path/to/js/files.js", "r")
  # 2. get the script content
  script_content = script.read()
  # 3. close the script file
  script.close()
  # 4. execute the script content and "print" "x" and "Hello World!"
  x = js.exec(script_content)
  </code></pre>
  
  <p>And, the expected result would look like this:</p>
  
  <pre><code>&gt;&gt;&gt; 5
  &gt;&gt;&gt; "Hello World!"
  </code></pre>