Running a python script in virtual environment with node.js pm2
<p>I would like to reference <a href="https://stackoverflow.com/questions/32127834/how-to-run-run-python-script-like-pm2-for-nodejs">this question</a> because I am certain that someone will flag this as a duplicate.</p>

<p>I am not looking for another reference to <a href="http://supervisord.org/" rel="noreferrer">supervisord</a>.  I'm sure that it is great and all, but the node PM2 has the functionality that I require and is more straightforward to implement and test.</p>

<h1>Manual Start</h1>

<p>During prototyping, I created a virtual environment called 'p3env'.  At the top of each script, I place a bash directive:</p>

<pre><code>#!./py3env/bin/python
</code></pre>

<p>This allows me to execute each script in the directory using this particular python environment without having to activate it.  It is very convenient and useful and the python script works well when I start it by hand.</p>

<p>I should be clear about what I mean when I say 'start it by hand'.  My script is called 'strain_to_db.py'.  When I start it by hand, I am on the shell via ssh:</p>

<pre><code>./strain_to_db.py
</code></pre>

<p>This gets everything working that I need to have working.</p>

<h1>PM2 Start</h1>

<h2>Moving from relative to absolute paths</h2>

<p>To get pm2 working, I started with:</p>

<pre><code>pm2 start ./strain_to_db.py
</code></pre>

<h2>Specifying the Interpreter</h2>

<p>Apparently pm2 ignores the directive at the top of the python script and attempts to execute using the global 'python'.  No problem, I can specify the interpreter:</p>

<pre><code>pm2 start ./strain_to_db.py --interpreter /home/ubuntu/db_if/p3env/bin/python
</code></pre>

<p>No dice.  Again, maybe try more absolute paths:</p>

<pre><code>pm2 start /home/ubuntu/db_if/strain_to_db.py --interpreter /home/ubuntu/db_if/p3env/bin/python
</code></pre>

<h2>Running script as command-line option</h2>

<p>Now I'm getting frustrated.  I try another tactic.  I attempt to run the python executable in the command line using:</p>

<pre><code>/home/ubuntu/db_if/p3env/bin/python /home/ubuntu/db_if/strain_to_db.py
</code></pre>

<p>This works fine when pm2 isn't involved.  When I try to pass this to pm2 using the 'command line argument' style:</p>

<pre><code>pm2 start /home/ubuntu/db_if/p3env/bin/python -- /home/ubuntu/db_if/strain_to_db.py
</code></pre>

<h2>Frustration</h2>

<p>Same error.  The error is always 'can't import pymysql', which is only installed on the virtual environment.</p>

<p>I am not sure where else to go with this.  I have several scripts that I want to add to the pm2 execution monitor, but I can't seem to get one of them to start and run correctly.</p>