WARNING: `pyenv init -` no longer sets PATH when starting the terminal window - python

Goal: I am trying to install the "pyenv" on my Linux machine with the help of this article "https://realpython.com/intro-to-pyenv/"
Expected Result: according the article pyenv should add to the "load path" of my Linux machine.
Actual Result: pyenv doesnt add to the load path and I have to manually add to the .bashrc file.
Error Messages: Actually I am not seeing any error messages for now. When I close my terminal window and open new one ; each time my terminal window opens up with the below message.
WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.
[centos#localhost ~]$
What I have done: I have searched through the SO for answers and I found my issue with some resolutions. but they didn't work for me as expected.
I have added the below lines to the .bashrc file of my user.
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Mind you that, even though this warning comes every time when I add 'pyenv' command to the terminal it shows me with the valid result.
for the references below is my .bash_profile and .bashrc files contents.
.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
#JAVA_HOME=/data/programs/jdk1.8.0_221
#JAVA_HOME=/data/programs/jdk-13.0.2
#JAVA_HOME=/data/programs/jdk1.7.0_80
JAVA_HOME=/data/programs/jdk-11.0.8
#JAVA_HOME=/data/programs/jdk-14.0.1
M2_HOME=/data/programs/apache-maven-3.6.1
JRE_HOME=$JAVA_HOME
export PATH JAVA_HOME M2_HOME JRE_HOME
export PATH=$JAVA_HOME/bin:$M2_HOME/bin::$PATH
export PATH
Also below is my .bashrc file contents.
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Environment: Centos 7
I want remove this error when I'm opening a terminal session every time. How do I resolve this ?

pyenv is upgraded to 2.0 and the init script is changed. Read from the official homepage but not some random article on the web.
Check README.md in pyenv on GitHub
Fix for your problem.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
NOTE: code above is not the whole pyenv init script, it only answers this very question. If you're setting up pyenv from the beginning, check pyenv's repo on Github for detail.

Just replace the line eval "$(pyenv init -)" from your ~/.bashrc file to eval "$(pyenv init --path)".

Related

Anyone know why my .bash_profile is adding "export pyenv" everytime i open terminal?

I had an issue where vscode was loading terminal in a blank screen and i got an error message in vscode saying "Unable to resolve your shell environment".
So i decided to check my .bash_profile file and was suprised to find it was over 700 lines where it was mainly just the following code repeated:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
I deleted the file and reopened terminal and realised everytime I open terminal, it adds the following lines:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
I installed pyenv straight forward following guides online so i'm not sure what time doing wrong.
If i delete .bash_profile then reopen terminal, it recreates the .bash_profile and adds the following code.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Anyone have any ideas how to fix this?
Note: I also have a .zhrc with the following exports which work as intended:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
export PATH=${PATH}:/usr/local/mysql/bin/
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
Check the other profiles that are loaded into the terminal, e.g. .bashrc and .profile (or .zshrc and .zprofile for zshell).
I had a similar issue where my .bashrc contained a script that would add the pyenv lines to another profile every time it was run.

How schedule crontab job when using pyenv and pipenv

I'm on Ubuntu 20.04, and I am having issues setting up the crontab correctly using pyenv + pipenv together. Simply adding pipenv run python script.py to the cronjob does not work; I think it may be due to:
the environment required by pyenv
the non-interactive shell of cronjob
UPDATED BIG QUESTION
How do I use /home/jennings/.pyenv/shims/pipenv correctly in crontab??
I've checked $? for pipenv -v in run.sh scheduled in crontab, and it fails.
https://github.com/pyenv/pyenv#advanced-configuration
00. (for reference) Bash startup
pyenv requires these entries in these startup files for interactive/login shells I don't understand how to translate this to a non-interactive cronjob call. How do I set up my BASH_ENV to emmulate these environents below?
https://stackoverflow.com/a/9954208/9335288
# ~/.profile
eval "$(pyenv init --path)"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init --path)"
fi
# ~/.bashrc:
export PYENV_ROOT="$HOME/.pyenv"
I. Crontab
I am trying to effectively write a .profile for the cronjob... but if you a better solution, please let me know.
# CRONTAB
SHELL=/bin/bash
BASH_ENV="home/jennings/.custom_bash_env
# BASH_ENV="home/jennings/.profile"
# BASH_ENV="home/jennings/.bashrc"
* * * * * cd $PROJECT_DIR; ./run.sh
# BASH_ENV:
Should I point to .profile or .bashrc instead?*
# PYENV
#eval "$(pyenv init --path)"
#if command -v pyenv 1>/dev/null 2>&1; then
# eval "$(pyenv init --path)"
#fi
# ENV VARIABLES
PYENV_ROOT="/home/jennings/.pyenv"
PYTHONPATH=$SOMEPLACE
PATH=all:of:them:paths
II. Project Folder
# run.sh:
#!/usr/bin/bash
# PYENV
eval "$(pyenv init --path)"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init --path)"
fi
# actual pipenv command I'm trying to run
pipenv run python main.py
# main.py:
import this
# Does some python and logging
What I've tried and know
Pointing directly at the .profile and .bashrc doesn't work
run.sh will run okay; it's thepipenv step that fails
Scheduling other non-pipenv commands work fine
pyenv init block
Placed in the BASH_ENV file, the cronjob doesn't run at all
Placed in the run.sh, the cronjob now runs, but the pipenv run still fails
pipenv related
I've tried pipenv shell; which python to use that one in the crontab enjoy -- no cigar
Cron has a limited environment. You have to declare the full path to the pipenv executable. Instead of using pipenv run python script.py you should use /usr/local/bin/pipenv run python script.py.
In your case the full path can be different which pipenv will give you the proper path.
This is explained in this post how to run pipenv in cronjob in ubuntu?

PATH="/custom/dir:$PATH" prepending instead of appending - Conda vs Pyenv

I have a Mac (latest software update) with Pyenv and Anaconda. I manage packages with Homebrew and have installed Python 3 with it. When I echo $PATH I get the following:
/Library/Frameworks/Python.framework/Versions/3.8/bin:
\ /usr/local/bin:
\ /usr/bin:
\ /bin:
\ /usr/local/sbin:
\ /usr/sbin:
\ /sbin:
\ /Library/TeX/texbin:
\ /Users/luca/.pyenv/versions/anaconda3-2020.02/condabin:
\ /Users/luca/.pyenv/versions/3.8.5/bin:
\ /Users/luca/.pyenv/bin
I do not know what sets up /Library/Frameworks/Python.framework/Versions/3.8/bin: this directory is non-existent on my Mac. I had previously installed Python without Homebrew that is why the directory was created. I did remove that, but there is still something that exports that line in $PATH, but I cannot find it! Does someone have a guess? I did try and grep -r /* it, but that is too much of a search for my laptop to finish.
I set up in my .zshenv the code for initialising pyenv and conda. Of course, I did write PATH="/dir/to/conda/bin:$PATH" and the same for pyenv (see code below). I do not understand why, but they end up at the end of $PATH. Does someone know why? Is it because of eval "$(pyenv init -)" being evaluated before conda?
# >>> pyenv initialize
export PYENV_SHELL=$SHELL
export PATH="/Users/luca/.pyenv/bin:$PATH"
export PATH="/Users/luca/.pyenv/versions/3.8.5/bin:$PATH" #export Python 3.8 bin directory
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
# <<< pyenv initialize <<<
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/luca/.pyenv/versions/anaconda3-2020.02/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/luca/.pyenv/versions/anaconda3-2020.02/etc/profile.d/conda.sh" ]; then
. "/Users/luca/.pyenv/versions/anaconda3-2020.02/etc/profile.d/conda.sh"
else
export PATH="/Users/luca/.pyenv/versions/anaconda3-2020.02/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
I have the exact same problem, and I could not find any other source on why the conda environment gets appended instead of prepended. I have added the following code to my .bash_profile (after conda initialisation) which flips the order of all path location. This is definitely a terrible practice, but it might be a quick fix for someone who is stuck on the same problem.
locations=($(echo $PATH | tr ":" "\n"))
path=""
for (( idx=${#locations[#]}-2 ; idx>=0 ; idx-- )) ; do
# Skip the last path location since this would put the general /Users/XXX/opt/anaconda3/condabin first
# To not do this change the -2 to -1
path="${path}:${locations[$idx]}"
done
path="${path:1}"
PATH=$path
export $PATH
As noted in the comment, I do not add the location "/Users/XXX/opt/anaconda3/condabin", since it would mess up my environment location, which is located second to last. Please examine your own path variable and decide accordingly.
EDIT: If anyone has a better solution, please let me know!
TL;DR
In macOS, if you place any code that modifies $PATH on macOS inside .zshenv, it will be overridden. You should put it in .zshrc or somewhere else which is then sourced by your .zshrc.
Complete explanation
As answered on Unix & Linux Stack Exchange, when zsh is sourced, files are read in the following order:
etc/zshenv/ -> $ZDOTDIR/.zshenv/ -> etc/zprofile/ -> $ZDOTDIR/.zprofile/ -> etc/zshrc/ -> $ZDOTDIR/.zshrc/ -> etc/zlogin/ -> $ZDOTDIR/.zlogin/
In macOS's /etc/zprofile, a script is sourced that overrides the $PATH according to the contents of these files:
/etc/paths
/etc/paths.d
/etc/manpaths
/etc/manpaths.d
So you should put not put any line that changes $PATH in your $ZDOTDIR/.zshenv, but rather in any file sourced after that (e.g. $ZDOTDIR/.zshrc). Do not edit files in /etc/!

Problems in activating conda enviroment in docker

I would like to set permanently a conda environment in my docker image in order that the functions of the conda package could be used by the script given as argument to the entrypoint.
This is the dockerfile that I created.
FROM continuumio/anaconda3
RUN conda create -n myenv
RUN echo "source activate myenv" > ~/.bashrc
ENV PATH:="/opt/conda/envs/myenv/bin:$PATH"
SHELL ["/bin/bash", "-c"]
ENTRYPOINT ["python3"]
It seems that the ~/.bashrc file is not sourced when I run the docker container. Am I doing something wrong?
Thank you
As a work around either use 'SHELL ["/bin/bash", "-i", "--login", "-c"]'
-or-
edit the .bashrc file in the image to not exit if not in interactive mode by changing "&ast;) return;;" to read "&ast;) ;;"
Using the first option bash will complain about job control and ttys, but the error can be ignored.
cause of the issue:
the .bashrc file contains the following command:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
which causes bash to stop sourcing the file if not in interactive mode. (the -i flag)
Unfortunately, I haven't found a way for the conda stanza to be inserted into .bash_profile or .profile automatically instead of (or in addition to) .bashrc, as there doesn't seem to be an option to override or add to the list of what files conda init examines for modification.

ZSH - pyenv 'command not found' on login or new window - works on load.

I'm using ZSH/oh-my-zsh and the regular OSX terminal, though the same problem occurs in iTerm. I'd been using rbenv and nvm without issue, but recently started working with python and pyenv, and have run into the following issue. On loading a new terminal window, I get the following message at the prompt:
Last login: Sat Apr 1 11:56:46 on ttys001
/Users/jackfuller/.zshenv:3: command not found: pyenv
Since installing pyenv, my machine seems noticeably slower. Obviously loading pyenv will slow things down but load times have dropped way off.
The catch is that pyenv works perfectly after the terminal is loaded, and as far as I can tell my .zshrc is configured properly:
alias dev="cd ~/development"
alias gow="cd ~/goworkspace"
alias dl="cd ~/downloads"
export PATH=/usr/local/bin:$HOME/bin:$PATH
export EDITOR='atom -n'
export PAGER='less -f'
export PATH=$HOME/.rbenv/shims:$PATH
RBENV
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
# NVM
export NVM_DIR="/Users/jackfuller/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
# PYENV
export PYENV_ROOT=/usr/local/opt/pyenv
eval "$(pyenv init - --no-rehash)"
# if which pyenv > /dev/null; then eval "$(pyenv init - --no-rehash)";
fi
# For go.
export GOPATH="$HOME/goworkspace"
export PATH=$PATH:/usr/local/go/bin
export GOROOT="usr/local/go"
If anyone can offer any advice/solutions, it would be much appreciated. Environment variables and shell config files seem more temperamental /confusing than they should be.
/Users/jackfuller/.zshenv:3: command not found: pyenv indicates that your error is in .zshenv at line 3. Maybe you could post your .zshenv. Is there a reason the fi after #PYENV is not commented? Also you could do export PATH=... once and not thrice. Try which pyenv to find the path to pyenv and look if its in your $PATH. For Future uses i'd put my aliases into ~/.zsh_aliases and do a source ~/.zsh_aliases in the .zshrc, otherwise it gets pretty ugly once you have some more aliases. Sorry for the bad structured answer ;)

Categories