virtualenvwrapper.sh crashes shell - python

I am following the install instructions for virtualenvwrapper, described here.
I have used pip install virtualenvwrapper and it installed at the path /home/.pyenv/shims/.
But when I run the command
source /home/.pyenv/shims/virtualenvwrapper.sh the whole Konsole shuts down.
I had previously put the command in the .bashrc file and almost broke Linux because the Konsole would crash immediately after opening.
I'm using Linux OpenSuse and Python version 3.6.0.
Any ideas what could be causing the crash?

I found a solution.
source ~/.pyenv/versions/VERSION/bin/virtualenvwrapper.sh works every time.
You can't use the shims directory for some reason. Maybe since virtualenvwrapper was likely installed into the pyenv version directory. If you use the shims directory, that link could break when switching versions with pyenv. It's better to access it directly.

I'm seeing the same thing on MacOS (10.12.6).
For me the .zshrc entry which closes/ends the terminal window is:
source $HOME/.pyenv/shims/virtualenvwrapper.sh
I also would like to know why this is occurring, and how to fix it.

I have come across this problem several times now on different machines and while I don't fully understand why it happens, I have found a solution to the problem.
The problem seems to be due to mismatches in the python version being used and the pip version used to install virtualenvwrapper. I had been using the system install of python (2.7) but the pip version was for python 3.5.
To fix this, use the suggestion in this answer as follows:
python -m pip install virtualenvwrapper
Then you can source /path/to/virtualenvwrapper.sh and everything should work fine.

Sean Combs' answer works for me, too. But I didn't want to hard code a specific python version, so I use grep to build the path to virtualenvwrapper.sh dynamically.
export VIRTUALENVWRAPPER_PYTHON=$HOME/.pyenv/shims/python
source $HOME/.pyenv/versions/$($VIRTUALENVWRAPPER_PYTHON -V 2>&1 | grep -Po '(?<=Python )(.+)')/bin/virtualenvwrapper.sh

To add to Sean's answer since I don't have enough reputation to add a comment, adding
export PYENV_VERSION="$(pyenv version-name)"
source ~/.pyenv/versions/$PYENV_VERSION/bin/virtualenvwrapper.sh
to your .bashrc (or .zshrc in my case) allows you to point to the correct virtualenvwrapper.sh even if you change python versions without hardcoding or grep

I had this problem on Mac and I found that zsh crashes
with line
source ~/.pyenv/versions/$PYENV_VERSION/bin/virtualenvwrapper.sh
if you not specified VIRTUALENVWRAPPER_PYTHON variable
So this's my .zshrc (or .bashrc) config for pyenv and virtualenvwrapper on fresh user
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PYENV_VERSION="$(pyenv version-name)"
VIRTUALENVWRAPPER_PYTHON="$HOME/.pyenv/shims/python"
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source ~/.pyenv/versions/$PYENV_VERSION/bin/virtualenvwrapper.sh
# Tell pyenv-virtualenvwrapper to use pyenv when creating new Python environments
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true"
# Set the pyenv shims to initialize
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi

I had a same problem, and I solved it by installing virtualenvwrapper with builtin python(e.g. /usr/bin/python3), not a pyenv python runtime, though the builtin one is not used in actual workspaces.

In your ~/.bashrc or ~/.zshrc, putting:
export PYENV_VERSION="$(cat $HOME/.pyenv/version)"
source "$HOME/.pyenv/versions/$PYENV_VERSION/bin/virtualenvwrapper.sh"
works for me.

Related

Make python3 as my default python on Mac

What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".

There is something wrong with virtualenvwrapper setting

I have installed python2 and python3 in my Mac osx and currently I'm using python3. And I am able to pip3 install virtualenv and virtualenvwrapper. The problem is whenever I restart my terminal, the settings for virtualenvwrapper don't work, like I can't use the commands mkvirtualenv and workon. The error I get is bash: the command workon is not found. I think there must be something wrong with my settings about the virtualenvwrapper. Here is what I try:
edit the ~/.bashrc:
export WORKON_HOME=~/Envs
source ~/Envs/django_projects/bin/virtualenvwrapper.sh
All my virtualenv packages are installed in the ~/Envs and after that, I source the .bashrc and it works, I could use workon and mkvirtualenv but once I reopen a new terminal it still could find't the workon command.
Does anyone know what's wrong with my setting?
After a few try, finally it works. There are two problems. The first one is because I have both python2 and python3 installed in my machine and by default the virtualenvwrapper try to run using the default python(in /usr/bin/python which is python2) so I have to change it to point to python3. So in my .bashrc file:
export WORKON_HOME=~/Envs
export VIRTUALENVWRAPPER_PYTHON=PATHS/TO/PYTHON3
source ~/Envs/django_projects/bin/virtualenvwrapper.sh
The second one is that .bashrc doesn't run automatically,we have to source in to ./bash_profile. Check https://apple.stackexchange.com/questions/12993/why-doesnt-bashrc-run-automatically for detail.
Then reopen the terminal and ta-da.
In order to be able to use virtualenvwrapper you need to source the script that is somewhere in /Users/mic4ael/Library/Python/{Python version}/bin/virtualenvwrapper.sh. So you could put source /Users/mic4ael/Library/Python/{Python version}/bin/virtualenvwrapper.sh to your .bashrc to make it every time you launch a terminal.

conda command not found. The .profile and "echo $PATH" seem however okay

I've blindly installed numerous times Python, admittedly without fully knowing what I was installing and where. I figured that now before I encounter even more problems that I need to tidy up my environment a bit. In Terminal, I used the command which -a python and came up with this:
/opt/local/bin/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
~/anaconda/bin/python
/opt/local/bin/python
/opt/local/bin/python
/opt/local/bin/python
/usr/bin/python
My .profile has:
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH="/opt/local/bin:/opt/local/sbin:/opt/local/lib/gmt4/bin:$PATH"
export PATH="~/teqc:$PATH"
export PATH="/usr/local/runpkr000:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
##
# Your previous ~/.profile file was backed up as ~/.profile.macports-saved_2015-04-02_at_16:40:23
##
# MacPorts Installer addition on 2015-04-02_at_16:40:23: adding an appropriate
PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
# added by Anaconda 2.3.0 installer
export PATH="~/anaconda/bin:$PATH"
And echo $PATH gives:
~/anaconda/bin:/opt/local/bin:/opt/local/sbin:/usr/local/runpkr000:~/teqc:/opt/local/bin:/opt/local/sbin:/opt/local/lib/gmt4/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:~/Library/TeX/texbin:/usr/texbin
I can't seem to locate where the problem is. I have been using pandas, numpy, matplotlib, scipy on my MBP for over a year and all seem to work fine (actually, the import pandas is always highlighted as red, but it works anyway). I've used Macports and Anacondas to install (as can be seen from the which -a python, it seems), and only just realised that only one of them was necessary to use. I'm a bit confused as to how to configure .profile and generally would like to tidy up this mess. My aim would be to then use conda to install another library (in this case obspy).
I followed the suggestion at the link
Where basically he has told to install anaconda using .sh variant instead of .pkg and it worked for me.
Hi instead of reinstalling everything you may add a symlink to conda (assuming everything is fine in your .profile or .bash_profile).
I did the following and worked for me:
ln -s "Applications/anaconda/bin/conda" /usr/local/bin/conda
Hope this helps
P.S. worked on OSX

How to add virtualenv to path

I am trying to find out why my virtualenv and/or virtualenv wrapper - installed using pip using homebrew - cannot be found. I think it's because it's not added to my PATH:
$ which virtualenv
$
and:
$ virtualenv someDir
$ -bash: virtualenv: command not found
I installed pip using homebrew, and virtualenv using pip, without problems. I tried reinstalling virtualenv, but that did not work either.
How do I know what path to add to PATH? Just the path that virtualenv.py seems to be installed into? That seems to be:
/usr/local/lib/python2.7/site-packages/virtualenv.py
I also found this guide, which suggests this:
$ ln -s ../Cellar/python/2.7/Frameworks/Python.framework/Versions/2.7/bin/virtualenv virtualenv
However, it does not help me run virtualenv. I am on Mac OSX 10.7.5 (Lion).
It seems that I myself am the exception to the rule for almost all 'simple' installation procedures. For some reason, it WAS a path related issue:
I ran brew info python, which outputted a lot of information. At the bottom I found this:
Executable python scripts will be put in:
/usr/local/share/python
so you may want to put "/usr/local/share/python" in your PATH, too.
I added that to my PATH in /etc/launchd.conf and ~/.bashrc and lo and behold:
$ which virtualenv
tells me:
"/usr/local/share/python/virtualenv"
I still don't know why I couldn't find any pointers in the right direction, online, anywhere? Is pip install virtualenv supposed to add to the PATH itself? If so, why not on my system? Why did #bibhas tell me explicitly it was not a path issue?
Had the same issue after pip install virtualenv
When I inspected python ls -la /usr/local/bin/python I found it was symbolically linked to /Library/Frameworks/Python.framework/Versions/2.7/bin/python
When I cd in that directory I also found the virtualenv executable and
Fixed it by
cd /Library/Frameworks/Python.framework/Versions/2.7/bin
ln virtualenv /usr/local/bin/virtualenv
Sidenote: I also happen to have a python installation in /System/Library/Frameworks/Python.framework/Versions/2.7/bin
I believe that's the one that came with OSX
In your .bashrc you need to have:
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
(Mac / Linux specific)
So I got an error message when I did a pip3 install --user --upgrade virtualenv telling me that I did not have Users/home/Library/Python/3.7/bin in my PATH. So I simply added it.
If this is on the Mac, the following did it for me
vi ~/.bash_profile
PATH="/Users/home/Library/Python/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
restart your terminal and type virtualenv env and that should do it.
I solved it by:
At first, I found out it is located at /usr/local/python3
and then I fixed it by the command:
ln virtualenv /usr/local/bin/virtualenv
This solution will give you an alternate tool to use and solve your virtualenv problem at the same time.
Use pythonbrew. It is inspired from rvm in the ruby world and is helpful in managing pythons on your system and also wrap virtualenv commands to provide virtual environment management. I use it Mountain Lion for my development purposes and have had no problems. More details (on my blog): http://stacktoheap.com/blog/2013/03/11/why-use-virtualenv-when-there-is-pythonbrew/
My idea is to add your virtualenv position to the BASH PATH
export PATH=$PATH:/usr/local/python2.7/bin
Or change your position
For those with Python 2.7, I came across this as well, and solved it by simply putting the following line into the \etc\paths file (may need to $ sudo chmod it first):
/Library/Frameworks/Python.framework/Versions/2.7/bin
Save the change and start a new Terminal session. Check it with echo $PATH
The module in /usr/local/lib/python2.7/site-packages is imported by a short script that uses pkg_resources.load_entry_point to run the application. The utility script should be in /usr/local/bin.

How to make Homebrew Python the main one I use? [duplicate]

I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with
brew install python
The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default
which python
give me
/usr/bin/python
and /usr/bin/python is not a symlink
How can I do to make python brew flavour to be my default python ?
As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
As you are using Homebrew the following command gives a better picture:
brew doctor
Output:
==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by
Homebrew. This is an issue if you eg. brew installed Python.
Consider editing your .bash_profile to put: /usr/local/bin ahead of
/usr/bin in your $PATH.
See: How to symlink python in Homebrew?
$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python
Quick fix:
Open /etc/paths
Change the order of the lines (highest priority on top)
In my case /etc/paths looks like:
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
If you want to know more about paths in OSX I found this article quite useful:
http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/
I did "brew install python" for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system's python.
When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:
/usr/local/bin $: ln -s python2 python
For Apple Silicon machines, the path are slightly different. After running brew install python, you must ensure your ~/.zshrc uses the correct Homebrew paths:
# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# Homebrew: Python
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"
Results:
% which python
/opt/homebrew/opt/python/libexec/bin/python
% python --version
Python 3.9.9
% which pip
/opt/homebrew/opt/python/libexec/bin/pip
% pip -V
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
python formula now uses python3(v3.6.5 for now), brew will link the directory:
/usr/local/opt/python -> ../Cellar/python/3.6.5
it will also link the binary:
/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3
If you still need to use python2.x, use:
brew install python#2
To use homebrew's python, just put its directory in PATH, for bash:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
for fish:
set -x PATH /usr/local/opt/python/libexec/bin $PATH
Note:
doing this will shadow the system default version of python
homebrew used to link python to /usr/local/share/python in older versions.
Homebrew does NOT replace stuff in "/usr/bin". You'll just want to put "/usr/local/bin" ahead of "/usr/bin" in your path, then "which python" will give you "/usr/local/bin/python".
Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.
Modify your $PATH, Add this in your bashrc or bash_profile:
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
more click here:
Issue #89791
I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.
You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.
Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You're just asking for trouble if you do.
python now points to python3, if you need python 2 then do:
brew install python#2 and then in your .zshrc or .bashrc file
export PATH="/usr/local/opt/python#2/libexec/bin:$PATH"
Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4.
That's the behavior I'm used to seeing in my terminal.
I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv --python option). Using tools like python_select is almost always a bad idea.
Use pyenv instead to install and switch between versions of Python. I've been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.
Consult pyenv's github page for installation instructions. Basically it goes like this:
- Install pyenv using homebrew. brew install pyenv
- Add a function to the end of your shell startup script so pyenv can do it's magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project's folder that specifies the python version. Pyenv will look override the global python version with the version in that file.
Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
After that, it should work correctly.
Just do:
brew install python
brew link python
After doing that, add this to your bashrc or bash_profile:
alias python='/usr/local/bin/python2'
Enjoy!
You can edit /etc/paths. Here is mine:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
Then add a symlink for the python version. In my case
$ cd /usr/local/bin
$ ln -s python3 python
Voila!
If you are fish shell
echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish
try this
which python3
Try typing python3 instead of just python
Greeting folks! I have the need to use python 3.10 version to harness its new features. My solved solution at 2022-07-03 is as follows. Have fun coding python!
» rm '/usr/local/bin/pip3.10'
» brew link python#3.10
» echo 'export PATH="/usr/local/opt/python#3.10/bin:$PATH"' >> ~/.zshrc
» python3
Python 3.10.5 (main, Jun 23 2022, 17:15:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Since High Sierra, you need to use:
sudo chown -R $(whoami) $(brew --prefix)/*
This is because /usr/local can no longer be chowned
brew link python
And you must create/add an alias for python and put it in your .zprofile (Located in Users/username folder, if you press Shift+command+.
This must point to your homebrew python installation location.
alias python ='opt/homebrew/bin/python3'
No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.

Categories

Resources