[PYTHON] Execute script using pyenv dependent module without entering pyenv environment

Notes for yourself

In python, you can create an independent python environment (hereinafter, pyenv environment) using virtualenv. It's very convenient, but scripts that use modules that depend on the pyenv environment can only be used after activating the pyenv environment. (Actually, it's not unusable, but it's a little annoying)

This is very inconvenient, but the information doesn't come in immediately. There is a lot of information on how to manage a lot of pyenv environments.

I looked into the Document to see if there was a good way to do it, and found an easy solution. https://virtualenv.readthedocs.org/en/latest/userguide.html#using-virtualenv-without-bin-python

In other words, it seems that you just need to add the following two lines to the beginning of the python module.

activate_this


activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

If you want to use numpy in the virtualenv environment as / tmp / pyenv as above

#!/usr/bin/env python

activate_this = '/tmp/pyenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import numpy as np

a = np.arange(10)

print(a)

[Conclusion] I'm talking about reading Document properly, but Python's 3rd Party module has little information in Japanese. It's pretty hard

Recommended Posts

Execute script using pyenv dependent module without entering pyenv environment
pyenv, virtualenv Use docker to manage the environment without using
Build a simple Python virtual environment without pyenv