Issue
I'd like to have an npm script in package.json that is like:
"scripts": {
"activate": "pyenv activate mypythonenvironment"
}
If I simply run
pyenv activate mypythonenvironment
in the bash shell terminal, it totally works.
However, if I run my script in the bash shell npm run activate
I get the error:
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Any help would be greatly appreciated! Thanks in advance.
EDIT: I am doing this for development purposes, so that I can run my backend from my frontend node directory for a quickstart on the project.
It seems that the problem is that the bash shell that npm scripts runs does not come equipped with all of the libraries available in my .bashrc.
Solution
Like the error message says, you failed to perform an initialization which your shell login script usually does when you are using the shell yourself. The activation probably looks something like eval "$(pyenv init -)"; eval "$(pyenv virtualenv-init -)"
; you can run that in your scripts
stanza, too:
"scripts": {
"activate": "eval \"$(pyenv init -)\"; eval \"$(pyenv virtualenv-init -)\"; pyenv activate mypythonenvironment"
}
but are you really sure requiring pyenv
is a good way to solve this problem in the first place? A regular virtual environment would probably work fine, and interfere less with whatever your user might already be using for other reasons.
Answered By - tripleee Answer Checked By - Dawn Plyler (WPSolving Volunteer)