pip fails in my python virtual environments - python

Trying to follow this tutorial: https://docs.python.org/3/tutorial/venv.html.
$ python3 -m venv tutorial-env
$ source tutorial-env/bin/activate
so far so good, however:
(tutorial-env) $ pip --version
import: not authorized `re' # error/constitute.c/WriteImage/1028.
import: not authorized `sys' # error/constitute.c/WriteImage/1028.
from: can't read /var/mail/pip
~/src/examples/tutorial-env/bin/pip: line 10: syntax error near unexpected token `('
~/src/examples/tutorial-env/bin/pip: line 10: ` sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])'
tells me something must be wrong with my python installation. I assume that should have worked.
Some information about my system installation, and the version of the software used to create that tutorial-env environment:
(tutorial-env) $ deactivate
$ which python3 && python3 --version
/usr/bin/python3
Python 3.5.2
$ python3 -m pip --version
pip 20.2.3 from ~/.local/lib/python3.5/site-packages/pip (python 3.5)
That does not seem right to me. Perhaps the problem is rooted in /usr/bin/python3 -m pip using dependencies under ~/.local/lib when I would have expected it to depend on /usr/local/lib/python3.5/dist-packages/pip. I'm not entirely sure this is the root cause though, perhaps I just don't know how to start a virtual environment.
Update: Looking carefully, I see that /usr/local/lib/python3.5/dist-packages does not contain a pip folder, whereas /usr/local/lib/python2.7/dist-packages by comparison does. Perhaps that is the root cause. However I have installed python3-pip via apt-get install.

Try re-installing both python and the virtual environment in that order

Related

Python and pip installation on Mac don't show version?

On Mac OS.
Downloaded the Python from their website.
Python -V return Python 2.7.16, and
python3 -V return Python 3.9.4
Installed pip with : python3 get-pip.py, got Successfully installed pip-21.0.1
But when I run pip -V
I get File "/usr/local/bin/pip", line 1.... SyntaxError: invalid syntax
After reading here a lot, i could not understand (in simple words for dumbs) :
How could you "alias" or update python to show/run in version 3+ ?
Why I can't get the pip version if it's installed ?
Use pip as a module instead
% python3 -m pip --version
pip 21.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Anyone who's interested , what i had to do is to
CD the project on terminal
run python3 -m venv env (this create virtual environment of python3 inside project)
run source env/bin/activate activate it.
now pip --version or python --version will return the right results.
From my basic knowledge i understand that the mac is using python2 which we don't want to interrupt, so we install python3 and create a 'virtual environment' for it inside our project folder, once its in, we can install all other things.
to deactivate (stop the virtual env) we run deactivate

zsh: /usr/local/bin/pipenv: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory

I recently switched from bash to zsh (MacOS). I haven't used pipenv since the switch. Now when I run any pipenv command I get the following error:
$ pipenv install
zsh: /usr/local/bin/pipenv: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory
I use pyenv for Python dependency management:
$ which python
/Users/ryan.payne/.pyenv/shims/python
My default python version is 3.7.3:
$ pyenv versions
system
2.7.16
3.6.8
* 3.7.3 (set by /Users/ryan.payne/.pyenv/version)
It seems like pipenv is not using my pyenv version of Python. How do I get pipenv working again?
You don't need to uninstall anything. Simply change the interpreter at /usr/local/bin and have your current python path in pyenv handy:
type python3
copy the path
vi /usr/local/bin/pipenv
It will look something like this:
Once it's changed, you will probably have to download pipenv again. Don't worry, your env is fine.
pip install pipenv
Go play in your env
I had this same error with awscli. The solution was to install python#3.7 via homebrew and then cp that installation into the directory awscli expected.
brew install python#3.7
cp -r /usr/local/opt/python#3.7/bin/python3.7 /usr/local/opt/python/bin/python3.7
Run in terminal:
brew install python3 && cp /usr/local/bin/python3 /usr/local/bin/python
After brew installation, sometimes it may not work.
Depending on whether you tried to install other python versions, the links might not be working any more, and therefore running the command
brew install python3 && cp /usr/local/bin/python3 /usr/local/bin/python
Might give an error as below
Error: Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists.
Try to remove the file 2to3
rm '/usr/local/bin/2to3'
and run the above code again
Alternatively, you can force the linkage
brew link --overwrite python#<version>
but you can first see the files that will be deleted by this forced linking using the command
brew link --overwrite --dry-run python#<version>
I hope this gives more light
If you installed pipenv with pipenv with pipx, then you can reinstall pipenv via pipx reinstall pipenv which should detect any top level changes in your python environment.

Are pip and python consistent? Seems like the answer is no. Can someone help me decrypt the documentation?

So i'm trying to implement stripe on a Django app and i'm having issues.
I installed Stripe using pip3 -install stripe and it downloaded. However when I run the server it says
ModuleNotFoundError: No module named 'stripe'
So looking around and on this I think I found some sort of an answer.
https://nomodulenamed.com/a/I-have-installed-the-package-using-pip#fail-to-install
Are pip and python consistent?
Seems like the answer is no.
pip3 -V returned pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
and
python3 -V returned Python 3.8.2
It seems that the easy fix is using python3 -m pip3 -V but that returns No module named pip3
and
python3 -m pip -V returns pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
but pip -V returns zsh: command not found: pip
which leaves me quite confused
more over i'm suppose to do # install your package
python -m pip <your-package-name>
so what goes in the place of
<your-package-name>
and I'm I suppose to use pip of pip3 since I use pip3 to install thing.
Since you can have more than one Python2 installation and more than one Python3 installation available on your machine, your question is better answered by understanding virtual environments.
It is precisely the reason why virtual environments exist!
when you create a python3 virtual environment there is no need to call pip3 as it is the default pip.
start by creating your virtual env (Assuming you have virtualenv installed ... if not install it on linux ubuntu by calling
sudo apt-get install virtualenv
sudo apt-get install python3-pip
python3 -m venv env
source bin/env/activate
pip install <yourpackage>
but I believe you are on macOS since you are getting zsh error,
fix your installation by using homebrew
brew install python3
pip3 install virtualenv
virtualenv -p python3 <path-to your-project>
source <path-to your-project>/bin/activate

python virtual env somehow using wrong pip

strugging to get pip install working with virtualenv.
$ virtualenv env
$ source env/Scripts/activate
I now have two python installations:
(env) $ where python
D:\my_folder\env\Scripts\python.exe
C:\Users\Me\Anaconda3\python.exe
I am using the one I want:
(env) $ which python
D:\my_folder\env\Scripts\python.exe
Same with pip:
(env) $ where pip
D:\my_folder\env\Scripts\pip.exe
C:\Users\Me\Anaconda3\Scripts\pip.exe
(env) $ which pip
D:\my_folder\env\Scripts\pip.exe
However I have the following
(env) $ pip -V
pip 18.0 from C:\Users\Me\Anaconda3\Lib\site-packages\pip (python 3.6)
wrong place!
(env) $ pip freeze
... dumps out my global packages!!
(env) $ pip install requirements.txt
... fails because requirements cannot be met.
I have a feeling that I have bad environment variable somewhere but don't really know where to look?! I'm just coming over from node and npm and finding python modules to be a bit of a car crash (although the language is awesome). Anyhow all help appreciated.
(EDIT)
OK, so (thanks to #alec_djinn) the following works:
python -m pip install requirements.txt
however
python -m pip freeze
still dumps my global installations.

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.

Categories

Resources