I recently started with virtualenv and was trying to install my own packages like PIL etc.
I wanted to know what do I need to do to make sure once I activate the environment, it remains activated or at least it is used,开发者_Go百科 instead of the system python.
So far I have created the environment in my home directory and can activate and deactivate it. I just need to make it permanent.
Please let me know if you need more information.
The general answer is "Place the 'bin' directory of the virtual environment in your path ahead of the system default". The specific answer on exactly how to do this depends rather dramatically on which OS and and shell you're using. Since you used the term "home directory", however, I'm going to assume you're using some form of Unix and, as most variants default to something bash-compatible, you probably just need to add the line "source /path/to/my/vritual/py/bin/activate" to your ~/.bashrc file. Add that line, logout, and log back in and your virtual environment should be your default.
Add this to the top of your file:
import sys, os
# make sure we are running the right version or venv
INTERP = os.path.expanduser("path/to/your/bin/python")
if sys.executable !=INTERP: os.execl(INTERP, INTERP, *sys.argv)
精彩评论