bash: virtualenv: command not found "ON Linux" - python

I am using a form of Lubuntu called GalliumOS (optimized for Chromebooks). I installed pip using $ sudo apt-get install python-pip. I then used pip install --user virtualenv and pip install virtualenv, and then when I tried to subsequently use virtualenv venv I experienced the message bash: virtualenv: command not found.
Between the pip installs above, I used pip uninstall virtualenv to get back to square one. The error remained after a reinstall.
I read several other posts, but all of them seemed to deal with similar problems on MacOS. One that came close was installing python pip and virtualenv simultaneously. Since I had already installed pip, I didn't think that these quite applied to my issue. Why is pip install virtualenv not working this way on LUbuntu / GalliumOS?

Are you sure pip install is "failing"? To me, it sounds like the directory to which pip is installing modules on your machine is not in your PATH environment variable, so when virtualenv is installed, your computer has no idea where to find it when you just type in virtualenv.
Find where pip is installing things on your computer, and then check if the directory where the pyenv executable is placed is in your PATH variable (e.g. by doing echo $PATH to print your PATH variable). If it's not, you need to update your PATH variable by adding the following to your .bashrc or .bash_profile or etc.:
export PATH="PATH_TO_WHERE_PIP_PUTS_EXECUTABLES:$PATH"

What finally worked for me was this. I used
$ sudo apt-get install python-virtualenv.
I was then able to create a virtual environment using $ virtualenv venv.
I was seeking to avoid using $ sudo pip install virtualenv, because of admonitions in other posts to not do this, and agreed, because of experiences I'd had with subsequent difficulties when doing this.

pip install virtualenv
This command worked for me for. This problem that raised to me on Kali Linux.

Related

Pip not working inside Virtual Env but works outside perfectly

Hello Guys I am tying to follow the installation here https://github.com/systers/portal and trying to deploy the server inside a virtual environment on my machine.
After lots of errors I decided to install a fresh copy of Ubuntu 16.04 and start
After the installation here are the things that I have installed using the given commands
I checked my current python and python3 versions using python --version and python3--version respectively and they are Python 2.7.12 and Python 3.5.2 respectively.
Easy Install. $ sudo apt-get install python-setuptools python-dev build-essential
pip. $ sudo easy_install pip
virtualenv. $ sudo pip install --upgrade virtualenv.
python3-dev tools.$sudo apt-get install python3-dev
Now after that I created a virtual env and activated it using the following commands
$ virtualenv venv1 --python=/usr/bin/python3
$ source venv/bin/activate
But now when I run the third command
$ pip install -r requirements/dev.txt
or even do
$pip --version
I get the error
bash: /media/rohan/New Volume/portal/venv1/bin/pip: "/media/rohan/New: bad interpreter: No such file or directory
Also in /venv1/bin the files pip,pip3 ,pip3.5 are present
I tried sudo easy_install pip thinking that it will install pip in the virtual environment but it installs to /usr/local/bin
Also I tried by creating a virtual env using the code
$virtualenv venv --python=/usr/bin/python
But that also doesnt work and this time also same error comes and in /venv/bin pip pip2 pip2.7 are present
PLEASE HELP
The problem appears to be that the path to your virtualenv has a space in it that isn't being escaped somewhere it should be.
Note the error you receive:
/media/rohan/New: bad interpreter: No such file or directory
So with that space in the path, it is trying to run a program that doesn't exist (/media/rohan/New) on a file that doesn't exist (Volume/portal/venv1/bin/pip).
Renaming New Volume to something without spaces like new_volume and then recreating a virtualenv should resolve this.
I had renamed the folder of virtual environment so that I was getting this error.
Then I renamed the venv folder name to the path mentioned in the error (That is the one which I had named while creating venv.) then tried to use pip and it worked.
Might be you have already solved your issue, but this is for the future visitors.

Broken references in Virtualenvs

I recently installed a bunch of dotfiles on my Mac along with some other applications (I changed to iTerm instead of Terminal, and Sublime as my default text editor) but ever since, all my virtual environments have stopped working, although their folders inside .virtualenvs are still there and they give the following error whenever I try to run anything in them:
dyld: Library not loaded: #executable_path/../.Python
Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
Reason: image not found
Trace/BPT trap: 5
I have removed all the files related to dotfiles and have restored my .bash_profile to what it was before, but the problem persists. Is there any way to diagnose the problem or solve it in an easy way (e.g. not requiring to create all the virtualenvs all over again)?
I found the solution to the problem here, so all credit goes to the author.
The gist is that when you create a virtualenv, many symlinks are created to the Homebrew installed Python.
Here is one example:
$ ls -la ~/.virtualenvs/my-virtual-env
...
lrwxr-xr-x 1 ryan staff 78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.7/Frameworks/Python.framework/Versions/2.7/Python
...
When you upgrade Python using Homebrew and then run brew cleanup, the symlinks in the virtualenv point to paths that no longer exist (because Homebrew deleted them).
The symlinks needs to point to the newly installed Python:
lrwxr-xr-x 1 ryan staff 78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/Python
The solution is to remove the symlinks in the virtualenv and then recreate them:
find ~/.virtualenvs/my-virtual-env/ -type l -delete
virtualenv ~/.virtualenvs/my-virtual-env
It's probably best to check what links will be deleted first before deleting them:
find ~/.virtualenvs/my-virtual-env/ -type l
In my opinion, it's even better to only delete broken symlinks. You can do this using GNU find:
gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete
You can install GNU find with Homebrew if you don't already have it:
brew install findutils
Notice that by default, GNU programs installed with Homebrew tend to be prefixed with the letter g. This is to avoid shadowing the find binary that ships with OS X.
After trying a few things, this worked for me:
go to your virtualenv directory (but don't run workon):
cd ~/.virtualenv/name_of_broken_venv
Now delete these files:
rm -rf .Python bin/python* lib/python2.7/* include/python2.7
Then to rebuild your venv, run:
virtualenv .
workon name_of_broken_venv
pip freeze
You should now see a list of your installed packages again.
This occurred when I updated to Mac OS X Mavericks from Snow Leopard. I had to re-install brew beforehand too. Hopefully you ran the freeze command for your project with pip.
To resolve, you have to update the paths that the virtual environment points to.
Install a version of python with brew:
brew install python
Re-install virtualenvwrapper.
pip install --upgrade virtualenvwrapper
Removed the old virtual environment:
rmvirtualenv old_project
Create a new virtual environment:
mkvirtualenv new_project
Work on new virtual environment
workon new_project
Use pip to install the requirements for the new project.
pip install -r requirements.txt
This should leave the project as it was before.
A update version #Chris Wedgwood's answer for keeping site-packages (keeping packages installed)
cd ~/.virtualenv/name_of_broken_venv
mv lib/python2.7/site-packages ./
rm -rf .Python bin lib include
virtualenv .
rm -rf lib/python2.7/site-packages
mv ./site-packages lib/python2.7/
It appears the proper way to resolve this issue is to run
pip install --upgrade virtualenv
after you have upgraded python with Homebrew.
This should be a general procedure for any formula that installs something like python, which has it's own package management system. When you install brew install python, you install python and pip and easy_install and virtualenv and so on. So, if those tools can be self-updated, it's best to try to do so before looking to Homebrew as the source of problems.
If this was caused by a brew upgrade that upgraded its Python, and you're ok with downgrading to the previous version, try brew switch python [previous version], eg brew switch python 3.6.5. From here.
Anyone who is using pipenv (and you should!) can simply use these two commands — without having the venv activated:
rm -rf `pipenv --venv` # remove the broken venv
pipenv install --dev # reinstall the venv from pipfile
virtualenvwrapper instructions
As indicated in the accepted answer, the root cause is likely a homebrew update that means your virtualenv symlinks are pointing at broken python paths - see details here.
For each virtual env, you need to reassign the symlinks to point at the correct python path (in brew cellar). Here is how to do it with virtualenvwrapper. Here I am updating a virtual env called "my-example-env".
cd ~/PYTHON_ENVS
find ./my-example-env -type l -delete
mkvirtualenv my-example-env
All done.
If you've busted python3 just try brew upgrade python3 that fixed it for me.
I recently faced this. None of the above solutions worked for me. Seems it wasn't actually Python's problem. When I was running aws s3 ls I was getting following error: dyld: Library not loaded: #executable_path/../.Python
This means, the library aws executable is pointing towards is either doesn't exist or is corrupted, thus I uninstalled and reinstalled aws-cli following instructions from this link and it worked!!
The problem for me(a MacOS user) is that brew updated the Python and virtualenvs links to the old version which was deleted.
We can check and fix it by
>> ls -al ~/.virtualenvs/<your-virtual-env>/.Python
.Python -> /usr/local/Cellar/python/<old-version>/Frameworks/Python.framework/Versions/3.7/Python
>> rm ~/.virtualenvs/<your-virtual-env>/.Python
>> ln -s /usr/local/Cellar/python/<new-version>/Frameworks/Python.framework/Versions/3.7/Python ~/.virtualenvs/<your-virtual-env>/.Python
I had a similar issue and i solved it by just rebuilding the virtual environment with virtualenv .
Using Python 2.7.10.
A single command virtualenv path-to-env does it. documentation
$ virtualenv path-to-env
Overwriting path-to-env/lib/python2.7/orig-prefix.txt with new content
New python executable in path-to-env/bin/python2.7
Also creating executable in path-to-env/bin/python
Installing setuptools, pip, wheel...done.
I had a broken virtual env due to a Homebrew reinstall of python (thereby broken symlinks) and also a few "sudo pip install"s I had done earlier. Weizhong's tips were very helpful in fixing the issues without having to reinstall packages. I also had to do the following for the mixed permissions problem.
sudo chown -R my_username lib/python2.7/site-packages
Virtualenvs are broken. Sometimes simple way is to delete venv folders and recreate virutalenvs.
If you using pipenv, just doing pipenv --rm solves the problem.
The accepted answer does not work for me: the file $WORKON_HOME/*/bin/python2.7 is no longer a symlink, it is a full-fledged executable:
$ file $WORKON_HOME/*/bin/python2.7
/Users/sds/.virtualenvs/.../bin/python2.7: Mach-O 64-bit executable x86_64
...
The solution is, alas, to completely remove and re-create from scratch all the virtual environments.
For the reference:
deactivate
pip install --user virtualenv virtualenvwrapper
pip install --user --upgrade virtualenv virtualenvwrapper
for ve in $(lsvirtualenv -b); do
# assume that each VE is associated with a project
# and the project has the requirements.txt file
project=$(cat $WORKON_HOME/$ve/.project)
rmvirtualenv $ve
mkvirtualenv -a $project -r requirements.txt $ve
done
Simply upgrading python3 worked for me:
brew upgrade python3
I tried the top few methods, but they didn't work, for me, which were trying to make tox work. What eventually worked was:
sudo pip install tox
even if tox was already installed. The output terminated with:
Successfully built filelock
Installing collected packages: py, pluggy, toml, filelock, tox
Successfully installed filelock-3.0.10 pluggy-0.11.0 py-1.8.0 toml-0.10.0 tox-3.9.0
What fixed it for me was just uninstalling python3 and pipenv then reinstalling them.
brew uninstall pipenv
brew uninstall python3
brew install python3
brew install pipenv
All the answers are great here, I tried a couple of solutions mentioned above by Ryan, Chris and couldn't resolve the issue, so had to follow a quick and dirty way.
rm -rf <project dir> (or mv <project dir> <backup projct dir> if you want to keep a backup)
git clone <project git url>
Move on!
Nothing novel here, but it makes life easier!
I am sure I am late to the party but I want to say that the resolution of this problem is much simpler than discussed here.
You can easily regenerate the virtual environment without having to delete/edit anything. Assuming that your broken environment is called env_to_fix you can just to the following:
mkvirtualenv env_to_fix
This will regenerate the links and fix the environment without the need to dump the current status somewhere and restore it.
I came across the same issue when I was pointing my python run time from 2 to 3 on my mac, pointing the alias python to python 3 path. I then recreate a new virtualenv and re-install those packages i need for my project. For my use case i have had a python program writing to google sheet. Clean up a few packages that are different from python 2 implementation and wa la, things started working again.
I was facing the same issue after upgrading brew on my OSX Catalina.
After trying bunch of stuffs, I find the following is the best and easy solution.
At first, delete the virtual env. (Optional)
find myvirtualenv -type l -delete
then recreate a new virtualenv
virtualenv myvirtualenv
Reference: https://www.jeremycade.com/python/osx/homebrew/2015/03/02/fixing-virtualenv-after-a-python-upgrade/
So there are many ways but one which worked for me is as follows since I already had my requirements.txt file freeze.
So delete old virtual environment with following command
use
deactivate
cd ..
rm -r old_virtual_environment
to install virtualenv python package with pip
use pip install virtualenv
then check if it's installed correctly
use virtualenv --version
jump to your project directory
use cd project_directory
now create new virtual environment inside project directory using following
use virtualenv name_of_new_virtual_environment
now activate newly created virtual environment
use source name_of_new_virtual_environment/bin/activate
now install all project dependencies using following command
use pip install -r requirements.txt
When you are running into this issue on a freshly created virtualenv, it might be that your python version installed by brew is "unlinked".
You can fix this for example by running: brew link python#3.8
(but specify your speficic python version)
You can also run brew doctor, it will tell you if you have unlinked stuff and how to fix this.

Error With Installing Virtualenv With Pip and Homebrew

I am trying to get a nice clean Python environment setup on OSX 10.9. I've installed Python with Homebrew and set my PATH variables so...
> which python
/usr/local/bin/python
and
> which pip
/usr/local/bin/pip
so when I look at my /usr/local/bin :
pip -> ../Cellar/python/2.7.6/bin/pip
python -> ../Cellar/python/2.7.6/bin/python
then when I run:
> pip install virtualenv
I get permission errors on /usr/local/bin/virtualenv:
...
running install_scripts
Installing virtualenv script to /usr/local/bin
error: /usr/local/bin/virtualenv: Permission denied
I thought that by using Homebrew I could use pip and avoid using sudo to install virtualenv. Am I doing something wrong here?
Ok! I managed to fix this myself.
I deleted all the virtualenv related things from /usr/local/bin (they had been installed under root for some reason and this was causing my permission issues.).
Then I did a pip uninstall virtualenv to get rid of other instances of virtualenv, as there was still one in /usr/local/lib/python2.7/site-packages/
Then a simple pip install virtualenv and things work fine now!
Most likely HomeBrew does some magic so that running brew install allows writing to /usr/local/bin, but this privilege is not available to normal commands. This is a guess, I didn't investigate this further.
Install virtualenv with brew:
brew install pyenv-virtualenv
This command:
pip install virtualenv
runs pip from your first directory from $PATH environment variable, which is usually system wide, thus when you run in this way - you are trying to install it globall.
You should install you your environment in your $HOME directory:
virtualenv $HOME/myvirpython
and later:
$HOME/myvirpython/bin pip install something
Additionally you should read this:
https://docs.brew.sh/Homebrew-and-Python

Pip install forced into global environment, when virtualenv is active

When trying (on a MacOS 10.8.5) to install packages into an activated virtual environment, pip fails with error:
source env/bin/activate
pip install somepackage
...
error: could not create '/Library/Python/2.7/site-packages/somepackage': Permission denied
I've been using pip for a long without this issue. There are lots of similar questions and the answers suggest to use sudo pip install ..., however so far could not find a way to fix pip so that packages go inside the virtual environment, instead of the global one.
Also, I've tried adding --install-option="--prefix=env", but ran into other error. This is really unusual, haven't seen this issue in years of using pip.
Does anyone have a clue? Thanks!
Self-answer. I've ran:
deactivate
virtualenv env
source env/bin/activate
pip install somepackage
That is created virtualenv right on top of the existing one - in the same directory and the problem was solved, even though I could not find an explanation.

Why would pip install python packages outside of my current virtualenv?

I just installed virtualenv earlier today, and I've been trying to get django-nonrel to work using a virtual environment. After many hours of trying and failing to do so, I chatted with someone who suggested that the problem is with pip itself.
I create a virtual environment with virtualenv env_name. I enter it with:
source env_name/bin/activate
I then install something with pip. For example,
sudo pip install git+https://github.com/django-nonrel/django-nonrel;
I then perform a pip freeze, and the only output is:
wsgiref=0.1.2
Can anyone see why this might happen?
Thanks,
ParagonRG
Notes:
pip is version 1.1.
virtualenv is version 1..7.1.2
You forgot the git extension
$> mkvirtualenv test
(test)> pip install git+https://github.com/django-nonrel/django-nonrel.git
(test)> pip freeze
Django==1.3.1
wsgiref==0.1.2
By the way, you dont need to use sudo if you work with virtualenv. The idea is having your virtualen somewhere you have permission like ~/.virtualenvs/

Categories

Resources