pyenv .zshrc producing errors on new set-up - python

I've been working on setting up virtual environments for the first time, and I'm running into a simple problem, but I'm running into roadblocks.
I've followed the step-by step process to enabling pyenv, and I believe that I have it correct with the exception of adding to the .zshrc file on macOS.
Using zsh, I've added these lines to the code:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
When I create a new py file, in Visual Code Studio, I see these errors:
bash: ‘export: command not found
source /Users/cnichols/.pyenv/versions/venv_one/bin/activate
bash: /Users/cnichols/.bash_profile: line 12: syntax error near unexpected token `then'
bash: /Users/cnichols/.bash_profile: line 12: `‘eval “export PATH="/usr/local/Cellar/pyenv-virtualenv/1.1.5/shims:${PATH}"; export PYENV_VIRTUALENV_INIT=1; _pyenv_virtualenv_hook() { local ret=$? if [ -n "$VIRTUAL_ENV" ]; then eval "$(pyenv sh-activate --quiet || pyenv sh-deactivate --quiet || true)" || true else eval "$(pyenv sh-activate --quiet || true)" || true fi return $ret }; typeset -g -a precmd_functions if [[ -z $precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then precmd_functions=(_pyenv_virtualenv_hook $precmd_functions); fi”’'
What exactly am I doing wrong here?

Related

Cant start my virtual environment of flask through cygwin

Cant start my virtual environment of flask through cygwin :
my system :(windows 10)
Method 1:
$ env/Scripts/activate
Method 2:
$ env/Scripts/activate.bat
Method 3:
$ source ./env/Scripts/activate
on my cygwin cli, but it gives the following error all the time :
-bash: $'\r': command not found
-bash: ./env/Scripts/activate: line 4: syntax error near unexpected token `$'{\r''
'bash: ./env/Scripts/activate: line 4: `deactivate () {
Note : The first two methods of activating virtual environment works nicely on VSCode but not on cygwin.
My activate file :
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r 2> /dev/null
fi
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "${1:-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="D:\projects\websites\googleHostFlaskApp\env"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH
# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
PS1="(flask) ${PS1:-}"
export PS1
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r 2> /dev/null
fi
As #Doug suggested , changing CR-LF to LF did worked.
The error was due to difference in line endings.
Unix systems (Linux and Mac) default to the LF (line feed) character for line breaks.
Windows on the other hand is “special” and defaults to CR/LF (carriage return AND line feed).
If you are using vscode like me: Then there is CRLF written at the bottom of vscode
By clicking there you can change activate file from CRLFto LF and it will remove the error you are facing.

how can I make usable source inside bash script?

I am writing a deployment script for Django, but at the moment to reload ~/.bashrc it doesn't found the SECRET_KEYbecause it didn't reload it, but I also tried with ., and source seems not to work. any other idea? my os is making use of bash
echo $0
bash
#!/bin/bash
token=$(/usr/bin/python3 token.py)
env=$(/usr/bin/python3 env.py)
echo -e " " >> ~/.bashrc
echo 'export EMAIL_HOST_USER="xxxxxxx' >> ~/.bashrc
echo 'export EMAIL_HOST_PASSWORD="xxxxxxx"' >> ~/.bashrc
echo 'export SECRET_KEY="'$token'"' >> ~/.bashrc
source ~/.bashrc
echo "$env" # line 19 error
output
deploy.sh: line 19: None: command not found
import os
# Get environment variables
SECRET_KEY = os.getenv('SECRET_KEY')
print(SECRET_KEY)

Unexpected end of file error when trying to restart shell after Google Cloud installation

I installed Google Cloud through my bash command and currently getting this error when I try to to restart my shell
bash: /Users/emm/.bash_profile: line 15: syntax error:
unexpected end of file
This is the command I typed in
exec -l $SHELL
Here is my .bash_profile
# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH
# added by Anaconda3 5.2.0 installer
export PATH="/Users/emm/anaconda3/bin:$PATH"
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/emm/my_app/googlecloud/google-cloud-sdk/path.bash.inc' ]; then . '/Users/emm/my_app/googlecloud/google-cloud-sdk/path.bash.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/Users/emm/my_app/googlecloud/google-cloud-sdk/completion.bash.inc' ]; then . '/Users/emm/my_app/googlecloud/google-cloud-sdk/completion.bash.inc';
Your .bash_profile is missing a fi at the end.
Each if needs a closing fi.
To fix the error run echo fi >> /Users/emm/.bash_profile once.
By the way:
You could improve your .bash_profile by using variables. Instead of repeating each path twice …
if [ -f 'longPath1' ]; then . 'longPath1'; fi
if [ -f 'longPath2' ]; then . 'longPath2'; fi
...
… you can write …
for p in 'longPath1' 'longPath2'; do
[ -f "$p" ] && .p
done
In your specific case you can even use brace expansion
for p in /Users/emm/my_app/googlecloud/google-cloud-sdk/{path,completion}.bash.inc; do
[ -f "$p" ] && .p
done

virtualenv will not activate in Cygwin inside PyCharm and breaks all unix-like commands after the failure

When trying to activate a venv within the terminal embedded in PyCharm, I recieve the following errors:
$ . venv/Scripts/activate
bash: cygpath: command not found
bash: basename: command not found
Furthermore, after this command fails all Unix-like commands are no longer recognized:
$ ls
bash: ls: command not found
()
I have tried using both Cygwin and Git-Bash as Unix-like terminals in PyCharm. I have also added C:\cygwin64\bin to my PATH variable. Virtualenv activates fine in both the standalone windows of Cygwin and Git-Bash.
Here are my settings in PyCharm for both Cygwin and Git-Bash:
"C:\Program Files\Git\bin\sh" -lic "cd ${OLDPWD-.}; bash"
"C:\cygwin64\bin\sh" -lic "cd ${OLDPWD-.}; bash"
Not sure if this is relevant, but I noticed that the cwd of the embedded terminal is a weird path:
$ pwd
/cygdrive/c/Users/account_name/Documents/python-projects/project
I have tried cd'ing into the 'normal' path but I have the same results.
I don't know enough about shell scripting to look at the activate file and really know what's going on. But I am 99% sure the issue is coming from these lines (mainly because cypath and basename are only found on these lines)
VIRTUAL_ENV="$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u 'C:\Users\account_name\Documents\python-projects\project\venv'; else echo '/C/Users/account_name/Documents/python-projects/project/venv'; fi;)"
export VIRTUAL_ENV
and
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
Then again, these are commands that come with cygwin (I think) so the Unix-like commands might be crashing out before this line.
All other commands seem to be working as intended, but everything stops working as soon as the failed activation happens. When I swap back to the Windows command prompt using cmd, my venv is immediately activated by PyCharm like normal.
Any input would be greatly helpful because I am at a loss!
Update:
Was able to get the . venv/Scripts/activate command to work. I decided to try and run the activate script piece by piece by pasting it into the terminal. Oddly enough, nothing crashed here...
Turns out for some reason if I swap the order of execution in the script, everything works.
If anyone could tell me how to avoid this, it would be greatly beneficial - at the moment I am going to have to manually adjust each activate scripts in all my virtual environments.
Working activate script:
VIRTUAL_ENV="$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u 'C:\Users\account_name\Documents\python-projects\project\venv'; else echo '/C/Users/account_name/Documents/python-projects/project/venv'; fi;)"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH
# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc
pydoc () {
python -m pydoc "$#"
}
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
unset -f pydoc >/dev/null 2>&1
# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
# deactivate nondestructive
Old activate script:
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
unset -f pydoc >/dev/null 2>&1
# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV="$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u 'C:\Users\account_name\Documents\python-projects\project\venv'; else echo '/C/Users/account_name/Documents/python-projects/project/venv'; fi;)"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH
# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc
pydoc () {
python -m pydoc "$#"
}
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
VIRTUAL_ENV="$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u 'C:\Users\account_name\Documents\python-projects\project\venv'; else echo '/C/Users/account_name/Documents/python-projects/project/venv'; fi;)"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH
# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc
pydoc () {
python -m pydoc "$#"
}
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
unset -f pydoc >/dev/null 2>&1
# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
# deactivate nondestructive

How can a shell function know if it is running within a virtualenv?

How should a bash function test whether it is running inside a Python virtualenv?
The two approaches that come to mind are:
[[ "$(type -t deactivate)" != function ]]; INVENV=$?
or
[[ "x$(which python)" != "x$VIRTUAL_ENV/bin/python" ]]; INVENV=$?
(Note: wanting $INVENV to be 1 if we're inside a virtualenv, and 0 otherwise, is what forces the backward-looking tests above.)
Is there something less hacky?
if [[ "$VIRTUAL_ENV" != "" ]]
then
INVENV=1
else
INVENV=0
fi
// or shorter if you like:
[[ "$VIRTUAL_ENV" == "" ]]; INVENV=$?
EDIT: as #ThiefMaster mentions in the comments, in certain conditions (for instance, when starting a new shell – perhaps in tmux or screen – from within an active virtualenv) this check may fail (however, starting new shells from within a virtualenv may cause other issues as well, I wouldn't recommend it).
Actually, I just found a similar question, from which one can easily derive an answer to this one:
Python: Determine if running inside virtualenv
E.g., a shell script can use something like
python -c 'import sys; print (sys.real_prefix)' 2>/dev/null && INVENV=1 || INVENV=0
(Thanks to Christian Long for showing how to make this solution work with Python 3 also.)
EDIT: Here's a more direct (hence clearer and cleaner) solution (taking a cue from JuanPablo's comment):
INVENV=$(python -c 'import sys; print ("1" if hasattr(sys, "real_prefix") else "0")')
If you use virtualenvwrappers there are pre/post scripts that run that could set INVENV for you.
Or what I do, put the following in your your .bashrc, and make a file called .venv in your working directory (for django) so that the virtual env is automatically loaded when you cd into the directory
export PREVPWD=`pwd`
export PREVENV_PATH=
handle_virtualenv(){
if [ "$PWD" != "$PREVPWD" ]; then
PREVPWD="$PWD";
if [ -n "$PREVENV_PATH" ]; then
if [ "`echo "$PWD" | grep -c $PREVENV_PATH`" = "0" ]; then
deactivate
unalias python 2> /dev/null
PREVENV_PATH=
fi
fi
# activate virtualenv dynamically
if [ -e "$PWD/.venv" ] && [ "$PWD" != "$PREVENV_PATH" ]; then
PREVENV_PATH="$PWD"
workon `basename $PWD`
if [ -e "manage.py" ]; then
alias python='python manage.py shell_plus'
fi
fi
fi
}
export PROMPT_COMMAND=handle_virtualenv

Categories