I am having trouble with pyenv after updating my macOS to 11.5. This is what I tried:
$ pyenv install 3.8.11 --skip-existing
$ pyenv virtualenv 3.8.11 >virtual env name<
$ pyenv local >virtual env name<
All of this works fine. Running pyenv local results in printing my virtual env's name. But I can't install dependancies or do anything with pip:
$ pip freeze
python >virtual env name< not installed
I tried following this answer, but I still have the same problem.
Note: This is also happening with virtual envs that were previously working pre-update.
Turns out the issue was with updating pyenv and not macOS. All I had to do was add the following lines to my ~./zshrc (or ~/.zprofile:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
Related
I have pyenv installed and have downloaded python versions 3.6.15 and 3.7.12. When I run
pyenv global 3.7.12
python -V
the output is: Python 3.10.2
pyenv versions gives the output
system
3.6.15
* 3.7.12 (set by /home/frege/.pyenv/version)
$ echo $PYENV_ROOT: /home/frege/.pyenv
$ which python: /user/bin/python
$ type python: python is hashed (/usr/bin/python)
I have the following in my .bashrc
export PATH="${HOME}/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
I have restarted my terminal and $PATH contains
/home/frege/.pyenv/plugins/pyenv-virtualenv/shims:
/home/frege/.pyenv/bin:
...
/usr/bin:
What is wrong?
I understand that getting pyenv and virtualenvs to work together can be problematic. I came from virtualenvwrapper which makes working on virtual envs simple and I found pyenv a bit of a culture shock.
I have finally got my workflow sorted and I thought I would post it here as it might help someone. My OS is Manjaro.
I installed pyenv:
curl https://pyenv.run | bash
The following was added to .bashrc:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/shims:${PATH}"
export PATH="$PYENV_ROOT/bin:$PATH"
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
export WORKON_HOME=$HOME/.virtualenvs
eval "$(pyenv init -)"
pyenv virtualenvwrapper_lazy
(I have not seen the line export PATH="$PYENV_ROOT/shims:${PATH}" in many explanations, but it is critical in mine.)
To invoke a global version of python:
pyenv shell <version>
or
pyenv shell system
To create a virtual env:
pyenv virtualenv <version> <env name>
To activate the virtual environment:
pyenv activate <env name>
To deactivate the virtual environment:
source deactivate
Starting out when I run $ which python3 I get:
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
And in my .zprofile I have:
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
So starting out, I have 3.8 installed and it works. Now, I need to create a setup that will allow me to install and use different versions of python for different projects.
So, I tried the following:
$ brew update
$ brew install pyenv
$ pyenv version
It printed out System. So, it only sees the python 2.7 that comes with macos. Then I tried:
$ pyenv install 3.9.1
$ pyenv install 3.8.7
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
$ source ~/.zshrc
Now, at the end of my .zshrc file I have:
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
But I still just get System when I run $ pyenv version. I know the different python versions are getting installed in ~/.pyenv/versions directory. So, what else am I supposed to do to get it
to list all the versions and switch back and forth between them?
First off, for pyenv to work correctly, you should uninstall the Python that's in /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 and remove it from your $PATH.
If you use pyenv to manage your Python versions, then you should not install Python in any other way. pyenv will update your path automatically as you switch Python version.
Next, you need to set pyenvâs default Python version by doing
$ pyenv global 3.9.1
on the command line.
Finally, to switch Python versions, you can use
$ pyenv local 3.8.7
on the command line. Note, that setting the local Python is sticky and works on a per-directory basis. So, for each directory where you need a specific Python version, use this command once.
For more info, see https://github.com/pyenv/pyenv/blob/master/COMMANDS.md
Cloud9 (an online ide) doesn't seem to support my virtual environment:
me:~/workspace/dir (master) $ source venv/bin/activate
(venv) me:~/workspace/dir (master) $ which python
/usr/bin/python
This same virtual directory worked fine on my local machine:
(venv) me$ which python
/Users/me/dir2/dir/venv/bin/python
How can I fix this?
The following works for me.
sudo apt-get install python3.5-venv
python3.5 -m venv --clear ./mypy3.5/
source ./mypy3.5/bin/activate
It uses the
(mypy3.5) $ which python
/home/ubuntu/mypy3.5/bin/python
But there is a gotcha which might have been your problem. The python3 -m venv uses soft links to how your python resolves in your environment. I had Python 3.3, 3.4 and 3.5 installed in /usr/local so the /usr/local/bin/python3 would change and break my Python3 venv. Note that "python3" is evaluated for the environment not for an absolute path. To be careful, when there are more than one Python 3 on you system, create your virtual environment with an explicit path like the following.
/usr/bin/python3.5 -m venv --clear ./mypy3.5/
source ./mypy3.5/bin/activate
ls -l $(which python3.5)
/home/ubuntu/mypy3.5/bin/python3.5 -> /usr/bin/python3.5*
I'm trying to run Jupyter notebooks with a globally installed version of Jupyter from within virtual environments (using virtualenvwrapper, because I want to manage versions of installed packages). And I do not what to use Anaconda.
The problem is when I run jupyter notebook from within the virtualenv, it cannot find the packages installed in the env, it only finds the packages installed globally.
How do I set up Jupyter to check for packages installed within the virtual environment instead of globally?
Here is what I get when I run which python and which jupyter:
globally:
which python >>> /usr/local/bin/python
which jupyter >>> /usr/local/bin/jupyter
from within virtualenv:
which python >>> /Users/brianclifton/.virtualenvs/test/bin/python
which jupyter >>> /usr/local/bin/jupyter
running jupyter notebook from within the virtualenv:
which python >>> /usr/local/bin/python
which jupyter >>> /usr/local/bin/jupyter
Also, here is my .bash_profile:
export VISUAL=vim
export EDITOR="$VISUAL"
export PS1="\\[\[\e[38;5;94m\][\u] \[\e[38;5;240m\]\w:\[\e[m\] \$(__git_ps1 '(%s)')$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
export PATH=/usr/local/bin/python:/usr/local/bin:$PATH
alias ls='ls -GFh'
alias pserv="python -m SimpleHTTPServer"
alias ipynb="jupyter notebook"
export WORKON_HOME=/Users/brianclifton/.virtualenvs
export PROJECT_HOME=/Users/brianclifton/dev
source /usr/local/bin/virtualenvwrapper.sh
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
alias branch='git rev-parse --abbrev-ref HEAD'
function frameworkpython {
if [[ ! -z "$VIRTUAL_ENV" ]]; then
PYTHONHOME=$VIRTUAL_ENV /usr/local/bin/python "$#"
else
/usr/local/bin/python "$#"
fi
}
Another solution from virtualenv doc
workon test
pip install ipykernel
python -m ipykernel install --prefix=/usr/local --name test-kernel
Then your kernel should appear when you run jupyter from your other virtualenv, and all the packages installed in test would be available from it. Change prefix value according to the doc if you prefer a per-user installation instead of system-wide
One possible solution is to prefix your virutalenv's bin directory to your path. This way jupyter will find the virtualenv's libraries. You can do this by running export PATH:`which python`:$PATH after you activate your environment. It would be easy enough to alias.
However, a better solution may be to add this line to the postactivate hook/script. To find the location of this script do ls $WORKON_HOME after activate virtualenvwrapper and edit $WORKON_HOME/<virtualenv_name>/bin/postactivate.
I am trying to create a virtual environment for Python 3.4 on a fresh install of Ubuntu Server 14.04. I following the instructions for the venv module at:
https://docs.python.org/3/library/venv.html#module-venv
I don't have a lot of Python 3.4 or Ubuntu experience.
When I type the command:
pyvenv testDir
I get back:
pyvenv: command not found
What is causing this?
Ubuntu 14.04 uses Python 2 by default, and the pyenv command does not exist in Python 2 out of the box.
You can, however, use virtualenv for the same purpose. You just need to install it!
You should:
Install Python 3 and virtualenv apt-get install -y python3 python-virtualenv
Create a Python 3 virtualenv: virtualenv -p $(which python3) testDir
Activate the virtual environment with source testDir/bin/activate
It's also possible to create virtualenv by python itself.
python3 -m venv myenv
see documentation https://docs.python.org/3/library/venv.html
It's in the package python3.4-venv (Linux Mint) or python3-venv (Ubuntu - I guess).
The advantages of venv over virtualenv are that (1) it's in vanilla Python3, (2) interpreter does retain tab-completion.
Edit the .bashrc file present in your home directory by adding below code and save the file:
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/home/'Enter systemname here'/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
After this you can now run the following command:
exec $SHELL
Now pyenv works properly