Reputation: 5300
In a Makefile, I call my python script using default python which has all my required modules as below
python test.py
It used to work but some other packages has added a new python to the system and changed the env PATH during build to use this new python and my Makefile started failing.
I could have hard coded correct python path as below but this is not a solution while distributing the package
/path/to/correct/python test.py
Is there a way to intelligently call a python interpreter which has all my modules. May be check all the python interpreters available in the system and test if they have necessary modules and then execute the script with that interpreter
Upvotes: 0
Views: 69
Reputation: 170798
After several years of python usage, I came to the conclusion that the best way to specify the Python interpretor is to use this shebang line:
#!/usr/bin/env python
With this Python 3 alternative
#!/usr/bin/env python3
Obviously, on Windows without Cygwin, you do have to assure that the "right" one is in PATH
.
And in order to install your requirements, you will have to run:
python -m pip -r requirements.txt
The command above is better alternative to 'pip' because it avoids the case where your pip executable may not really install stuff inside the same python environment that you are wishing for.
Another solution would be to use virtual enviroments, but this may be too much for quick start.
Upvotes: 0
Reputation: 7980
Using a combination of which -a python
a for loop through those results and a simple test expression to import the needed packages should do the trick. Wrap all that up in a Makefile and you are done.
Here are a few examples from my machine you can use to build up a solution.
$ which -a python
/usr/miniconda/bin/python
/usr/bin/python
$ python -c 'import requirement' > /dev/null 2>&1 && echo "This one works" || echo "This one does not work"
This one does not work
$ python -c 'import sys' > /dev/null 2>&1 && echo "This one works" || echo "This one does not work"
This one works
Of course, it may be best to do something like $(PYTHON) test.py
in your Makefile and then invoking with make PYTHON=/path/to/correct/python
may be simpler and less error prone.
Upvotes: 2