Pip is not found after creating new python virtual environment - python

I'm trying to create new Python virtual environment, but I have problems with pip.
I want my virtual environment to run on Python 3.10, but my default system Python version is 3.9.5. So I downloaded Python 3.10 with
sudo apt-get install python3.10
and then i run
python3 -m virtualenv venv --python=python3.10
to create virtual environment.
The problem is, when I try to use pip I get an error:
Traceback (most recent call last):
File "/home/<my user name>/venv/bin/pip", line 5, in <module>
from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'
I tried running
python -m ensurepip --upgrade
but got an error:
/home/pablo/venv/bin/python: No module named ensurepip
Also running whereis pip shows, that there are some pips in the virtual environment.
pip: /usr/bin/pip /home/<my user name>/venv/bin/pip3.10 /home/<my user name>/venv/bin/pip /usr/share/man/man1/pip.1.gz
Also, when I create virtual environments with my default python interpreter, everything works perfectly fine.

I would recommend using pyenv with pyenv-virtualenv. In my experience it's just too much work to go manual. Sorry I know this is not a true answer, but having wasted hours and hours on similar issues, I feel like sharing )

your installed binary executable is named as pip3.10(/usr/bin/pip /home//venv/bin/pip3.10). So you should use the same name to call it, just pip wouldn't work. Have you tried that?

Related

pipenv, jupyter and possibly other commands stopped working after upgrading python3.7 to python3.8

I upgraded python version from 3.7 to 3.8 using brew upgrade python3 however after upgrading pipenv and jupyter commands stopped working. These are the commands I've ran as of now other commands may not be working as well.
This is the output when I ran pipenv:
-bash: /usr/local/bin/pipenv: /usr/local/opt/python/bin/python3.7: bad interpreter: No such file or directory
This is the output when I ran the jupyter command:
-bash: /usr/local/bin/jupyter: /usr/local/opt/python/bin/python3.7: bad interpreter: No such file or directory
So I opened up the file /usr/local/bin/pipenv and the interpreter declared at the top is #!/usr/local/opt/python/bin/python3.7
So I changed it to #!/usr/local/opt/python/bin/python3.8 and ran pipenv command and this is the output:
Traceback (most recent call last):
File "/usr/local/bin/pipenv", line 5, in <module>
from pipenv import cli
ModuleNotFoundError: No module named 'pipenv'
I ran pip3 list command and this was the output
Package Version
---------- -------
pip 20.1.1
setuptools 49.2.0
wheel 0.34.2
So i went into /usr/local/lib and there were 2 folders named python3.7 and python3.8. The site-packages folder in python3.7 has all the modules I had previously installed but the site-packages folder in python3.8 contains only 3 modules which I got using pip3 list.
I don't fully understand the issue but I think all the older commands are still linked to python3.7 interpreter which no longer exists and secondly all the site packages need to be in /usr/local/lib/python3.8
Yes, site-packages are per-interpreter-version. (Also, virtualenvs tend to break when you upgrade your Python version.)
Just reinstall the packages you need with your new Python 3.8 based pip3.

Using Fabric 2 and Fabric 3 in the same project

I am currently using Fabric3 in a python project and I want to use the azure-cli package. However when I install the package, it appears to break my Fabric3 install:
root ~ $ fab --version
Traceback (most recent call last):
File "/opt/rh/rh-python36/root/usr/bin/fab", line 7, in
from Fabric3.main import program
ModuleNotFoundError: No module named 'Fabric3'
I noticed that the pip install of azure-cli appears to install fabric (2.5.0), and this appears to overwrite some of the fabric files within the "/opt/rh/rh-python36/root/usrbin/fab" script and the "/opt/rh/rh-python36/root/usr/lib64/python3.6/site-packages/fabric/" directory.
So my question is, is there a way to run different versions of Fabric within the same project?
Thanks in advance
I found a way to do this by using python virtual environments.
I created a virtual env, activated the virtual env, installed the pip package, deactivated the virtual env, then used Fabric 3 to call the function in the venv.
python -m venv azure_cli_venv
. azure_cli_venv/bin/activate
pip install azure-cli
deactivate
Fabric3 local call:
local(f'./azure_cli_venv/bin/az --help

Python venv (virual environment) uses wrong version of Python

I have both Python 3.7 and 3.6 installed. But in my profile I set to use 3.6 and this is indeed a version that gets reported by which python3.
$ which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
I then create virtual env by running python3 -m venv venv
And everything in my venv gets linked to the 3.6. I confirm this with ls -la venv/bin and by inspecting venv/pyvenv.cfg.
I activate the venv virtual environment.
I then install a zappa library with pip install zappa.
And when I run zappa deploy, I get an error that Python 3.7 is not supported. Why would a library use the version of Python that is not configured within the virtualenv and how do I make it use the correct version?
(venv) $ zappa deploy
Traceback (most recent call last):
File "/Users/t/Projects/djzappa/Spheres/venv/bin/zappa", line 7, in <module>
from zappa.cli import handle
File "/Users/t/Projects/djzappa/Spheres/venv/lib/python3.7/site-packages/zappa/__init__.py", line 12, in <module>
raise RuntimeError(err_msg)
RuntimeError: This version of Python (3.7) is not supported!
Zappa (and AWS Lambda) support the following versions of Python: ['2.7', '3.6']
NOTE: I do NOT use virtualenv as everybody is suggesting, and as other existing answers are referring.
Use this when you create environment:
virtualenv --python=/usr/bin/python3.6
But if you want to use venv, first of all, check whether python3 in your terminal refers to the python3.6 as you want.
If not - run your command with the full path to python3.6 instead of sole python3 in python3 -m venv venv.
Edit: why your alias python3 is referring to python3.7 - it probably depends on OS you use and sequence of installation. Hard to say.
As an addition to the accepted answer, be also aware that changing the directory name where your venv is located causes using the default python and pip paths of your system, instead of using the venv one.
The Python version is different from the virtualenv python version and that is why it is complaining.
You can recreate your virtual environment(delete the existing one) and use the following command
virtualenv --python=your python version here venv
Or
mkvirtualenv venv --python=your python version here
if you want to create your virtual environment using mkvirtualenv command.

ImportError when using virtualenv in Django

My Question is pretty Simple.
Whenever I use virtualenv environment in my Django project it gives the following error when executed this command
python manage.py runserver
File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
But when i use root and give the same command it seems to work perfectly. Why does this Happen? I am in the virtualenv so it means i am the root person by default.How can i fix this error?
I had seen many forums but couldn't find related to mine.
This happens because you have installed django in the system-wide Python interpreter; possibly by doing something like sudo pip install django.
Once you create a virtual environment and activate it - it contains no packages. The concept of a virtual environment is that it allows you to install Python packages without affecting the global Python installation.
So once you activate a virtual environment, you have to install packages in that virtual environment; so you should pip install django (note: without sudo) once you activate the virtual environment; like this:
$ virtualenv sample_env
...
$ source sample_env/bin/activate
(sample_env) $ pip install django
A virtual environment is a virtual environment for Python; it does not control what the user is that is logged into the system.
Finally, as a general rule - you should not be using root for development purposes as doing so can easily compromise your system.
Yup i have same error while running Django. I also have enabled virtual environment. But I was still getting error.
Solution to this problem is using this command to install any python package
python -m pip install django
This will definitely solve your problem. As it solved mine.

Why am I getting ImportError: No module named pip ' right after installing pip?

I have installed pip and ez setup. I also checked the system path and I can see the module in the folder structure. Still when i try to run pip command, I get an Import error saying no module named pip. I am running 32bit python on a windows7 machine
Just be sure that you have include python to windows PATH variable, then run python -m ensurepip
After running get_pip.py with python embed you have to modify your pythonXX._pth file. Add Lib\site-packages, to get something like this:
pythonXX.zip
.
Lib\site-packages
# Uncomment to run site.main() automatically
#import site
If you don't you will get this error:
ModuleNotFoundError: No module named 'pip'
or
python-3.8.2-embed-amd64\python.exe: No module named pip
λ pip
Traceback (most recent call last):
File "runpy.py", line 193, in _run_module_as_main
File "runpy.py", line 86, in _run_code
File "python-3.8.2-embed-amd64\Scripts\pip.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'pip'
λ python -m pip
python-3.8.2-embed-amd64\python.exe: No module named pip
This issue occurs with me while I was trying to upgrade pip version.
It was resolved with the following commands:
python -m ensurepip
The above command restores the pip and below mentioned upgrades it.
python -m pip install --upgrade pip
What solved the issue on my case was go to:
cd C:\Program Files\Python37\Scripts
And run below command:
easy_install.exe pip
Follow steps given in https://michlstechblog.info/blog/python-install-python-with-pip-on-windows-by-the-embeddable-zip-file/. Replace x with version number of Python.
Open the pythonxx.__pth file, located in your python folder.
Edit the contents (e.g. D:\Pythonx.x.x to the following):
D:\Pythonx.x.x
D:\Pythonx.x.x\DLLs
D:\Pythonx.x.x\lib
D:\Pythonx.x.x\lib\plat-win
D:\Pythonx.x.x\lib\site-packages
try to type pip3 instead pip.
also for upgrading pip dont use pip3 in the command
python -m pip install -U pip
maybe it helps
turned out i had 2 versions of python on my laptop
both commands worked for me
python -m ensurepip
py -m ensurepip
both with another installation path
c:\tools\python\lib\site-packages
c:\program files (x86)\microsoft visual studio\shared\python36_64\lib\site-packages
only the first path was in my %PATH% variable
First make sure that python is added in environment variable.
Try checking the version of pip or pip3. Use these commands to check.
For pip:
pip --version
For pip3:
pip3 --version
If you can see any version of pip and still not able to use it, then run the following command.
python -m ensurepip
This ensures the pip in your system.
Running these 2 commands helped me:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
If you wrote
pip install --upgrade pip
and you got
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.2.1
Uninstalling pip-20.2.1:
ERROR: Could not install packages due to an EnvironmentError...
then you have uninstalled pip instead install pip.
This could be the reason of your problem.
The Gorodeckij Dimitrij's answer works for me.
python -m ensurepip
I found this post while looking for a solution for the same problem. I was using an embedded python distribution. In this case, the solution is to uncomment import site in the file python<version>._pth.
The ensurepip module was added in version 3.4 and then backported to 2.7.9.
So make sure your Python version is at least 2.7.9 if using Python 2, and at least 3.4 if using Python 3.
I'v solved this error by setting the correct path variables
C:\Users\name\AppData\Local\Programs\Python\Python37\Scripts
C:\Users\name\AppData\Local\Programs\Python\Python37\Lib\site-packages
I was facing same issue and resolved using following steps
1) Go to your paython package and rename "python37._pth" to python37._pth.save
2) curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
3) then run python get-pip.py
4) pip install django
Hope this help
I've solved this error downloading the executable file for python 3.7.
I've had downloaded the embeddeable version and got that error.
Now it works! :D
The method I'm going to tell might not be the correct way to do it. But this method solved my issue. I tried every solution on youtube and StackOverflow methods.
If you have two python versions installed. Delete one. I have the python 3.8.1 and 3.9.0 versions installed. I deleted version 3.9.0 from the C directory.
Now go to the control panel > System and security > System > Advanced system settings.
Click on 'environment variables'.
Select the path and click on 'edit'
Now, add the path of the python and also the path of pip module. In my case it was c:\python38 and c:\python38\scripts
This method solved my issue.
Instead of Python zip install python by python installer. It fixed the issue for me.

Categories

Resources