recently I have updated my default python to be 3.10 but I realized that some parts of my OS started to broke;
I can use python -m venv venv and it works like a charm, but if I try to activate a pipenv shell I do receive some errors
It has started by raising errors in the collections.MutableMapping so I changed it to collections.abc.MutableMapping... But it doesn't stop raising errors and I can't remove the pipenv.
When I try to pipenv shell or even pipenv --rm I do receive the same message error
ImportError: cannot import name 'InvalidHeader' from 'requests.packages.urllib3.exceptions' (/usr/lib/python3/dist-packages/pipenv/vendor/requests/packages/urllib3/exceptions.py)
As I can't find references on how to fix this, any help you can provide will be really appreciated guys.
Regards,
Leonardo
Running with a new Ubuntu 22.04 system, I faced the same urllib3 collections issue you report as your initial problem:
$ pipenv install
Traceback (most recent call last):
<snip>
File "/usr/lib/python3/dist-packages/pipenv/vendor/requests/packages/urllib3/_collections.py", line 2, in <module>
from collections import Mapping, MutableMapping
ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
I found a discussion of this in the pipenv github's issue page here:
https://github.com/pypa/pipenv/issues/5088
Apparently there is a problem with Python 3.10 with the apt-installed version of pipenv. I suppose the apt repositories should obtain an updated pipenv? In any case, taking the input from amod-amatya, I installed pipenv via pip (on my system, my packages are installed via sudo, but running as a non-sudo user should work as well):
sudo pip install --upgrade pipenv
FWIW, I didn't uninstall the apt version of pipenv. With that pip-installed pipenv version, pipenv now works for me:
$ pipenv --version
pipenv, version 2022.5.2
$ pipenv install
<snip>
Building requirements...
Resolving dependencies...
_ Success!
Updated Pipfile.lock (1d3b5e)!
Installing dependencies from Pipfile.lock (1d3b5e)...
__ ________________________________ 3/3 _ 00:00:01
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
Related
I had installed virtualenv by sudo -H pip3 install virtualenv, and created a venv by virtualenv, but now I can't create env in my new Django project that includes Pipfile.
I tried:
$ pipenv shell
But its result is:
Creating a virtualenv for this project...
Pipfile: /home/mostafa/py38/lib/python3.8/site-packages/carfix/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
⠹ Creating virtual environment...ModuleNotFoundError: No module named 'virtualenv.seed.via_app_data'
✘ Failed creating virtual environment
I tried these answers but the problem is not yet resolved:
Answer 1
I finally found the solution to the problem:
I Used poetry lockfor one app and everything worked fine!
I Used poetry lock for a second app and received the following error message:
$ poetry lock
Creating virtualenv mytestapp-vm7OCEgV-py3.8 in /home/alexb7217/.cache/pypoetry/virtualenvs
ModuleNotFoundError
No module named 'virtualenv.seed.via_app_data'
at <frozen importlib._bootstrap>:973 in _find_and_load_unlocked
Followed the most simple suggestion:
$ sudo apt remove --purge python3-virtualenv
Re-ran poetry-lock
$ poetry lock
Updating dependenciesResolving dependencies... (1.1s)
Everything works great, that's it! ;)
The solution is to remove the installed virtualenv using
pip3 uninstall virtualenv
And use the default installation of virtualenv then :
pipenv shell
Try to remove your virtualenv installation and try to install it via:
sudo apt-get install virtualenv.
Then create virtualenv with:
virtualenv virtualenv_dir
Where virtualenv_dir is your Django project folder
I had the same issue. I was not able to select an existing environment nor create one.
What I did was to create one from the terminal:
virtualenv venv
get into it
. venv/bin/activate
install the requirements I had, with pip3 no pip:
pip3 install -r requirements.txt
and when I went back to pycharm, it asked me if I wanted to use that environment I just created.
I am trying to use the requests module, here is how I installed it:
[ec2-user#ip-xxx-xx-xx-xxx newslookup]$ pipenv install requests
Creating a virtualenv for this project...
Pipfile: /var/www/html/newslookup/Pipfile
Using /usr/bin/python2 (2.7.14) to create virtualenv...
⠇ Creating virtual environment...Already using interpreter /usr/bin/python2
No LICENSE.txt / LICENSE found in source
New python executable in /home/ec2-user/.local/share/virtualenvs/newslookup-5acwuw4D/bin/python2
Also creating executable in /home/ec2-user/.local/share/virtualenvs/newslookup-5acwuw4D/bin/python
Installing setuptools, pip, wheel...
done.
✔ Successfully created virtual environment!
Virtualenv location: /home/ec2-user/.local/share/virtualenvs/newslookup-5acwuw4D
Creating a Pipfile for this project...
Installing requests...
Adding requests to Pipfile's [packages]...
✔ Installation Succeeded
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
✔ Success!
Updated Pipfile.lock (ab273c)!
Installing dependencies from Pipfile.lock (ab273c)...
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:01
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
Here is what it looks like when I run my script:
[ec2-user#ip-xxx-xx-xx-xxx newslookup]$ python3 nasdaq_scrape_sec.py
Traceback (most recent call last):
File "nasdaq_scrape_sec.py", line 5, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
As you can see, the problem is that when I install it, it is using python 2 instead of python 3 (Using /usr/bin/python2 (2.7.14))
How can I tell pipenv to use Python 3 instead of Python 2?
Or is there a specific python 3 way of installing requests?
The easiest way is to run (assuming you want 3.6):
pipenv --python 3.6
Alternatively and as a more persistent solution, you can add the following lines into the pip file:
[requires]
python_version = "3.6"
While creating a environment ,Pipenv will automatically scan your system for a Python that matches that given VERSION.
pipenv --python VERSION
just call the pipenv from python3, it will do the job
python3 -m pipenv install requests
I have installed virtualenv
sudo pip3 install virtualenv
Then created a new virtualenv
virtualenv --python=/usr/bin/python3.5 venv
It returned
Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in /home/alexandra/Documents/online-store/venv/bin/python3.5
Also creating executable in /home/alexandra/Documents/online-store/venv/bin/python
Installing setuptools, pip, wheel...done.
Activated it
source venv/bin/activate
Then installed Django
sudo pip3 install django
It installed there (that is what sudo pip3 install django --upgrade returned)
/usr/local/lib/python3.4/dist-packages
Maybe that's the problem? Because it installed in python3.4 and not python3.5. But how to install Django in python3.5?
In file manage.py there is a path to python3.5 in this virtualenv.
Then I run
python manage.py runserver
It returns an error
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django'
Can anyone help me?
When using pip to install packages inside the virtual env, do not sudo. It makes packages be installed globally. Simply remove sudo from beginning of the command and it should install it in the venv you are currently working on.
You installed django into a system python 3.4.
When you ran install command first time without sudo, did it complain about permissions? That should have been your clue that it's not the right pip. My guess is pip3 only exists in system python and not your virtualenv. You can use which to find out, but this will work in any case:
venv/bin/pip install django
Don't do sudo and just use pip for installing all the packages. You have already installed python3 in your virtualenv, so there is no need of using pip3.
Just do e.g. pip install django. or for other packages pip install <package_name>
I am following the django quick install instructions.
I activated the venv
I did pip install django from inside the venv, with and without sudo and receive the same message.
To check the install:
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'django'
"did you...?"
No. I didn't. I didn't do anything extra. This is a new spawning of a vm (built by vagrant/puppet) that already has virtualenv installed. The only thing I had to do to prep this venv was to make it not use the systems python 2.7.8. virtualenv --python=/usr/bin/python3.4 myvirtualenv
(On a separate pyramid-based project, it would fail during setup (python setup.py development.ini would fail with invalid command name 'development.ini' I don't need that project for this project, but saying this here may reveal something about the install.).
So how do I make this go away?
Edit: clarified for answers from #catavaran & #Burhan Khalid
I suspect here is where you went wrong:
You created a virtual environment (virtualenv --python=/usr/bin/python3.4)
Next, you forgot to activate it; or you activated it and then you did sudo pip install django.
sudo pip will run the system wide pip, installing django for Python 2.7.8, rather than for the virtual environment.
If you exit the virtual environment and try to import django - it will work.
To fix the problem, make sure you activate the virtual environment and then do not use sudo to install packages.
Before installing/using django you should activate your virtualenv. And do not sudo while you call pip:
$ source ~/.virtualenvs/myenv/bin/activate
$ pip install django
Turns out the problem was permissions earlier in the setup.
It leads to Django being unable to complete it's install. pip install django would fail with a permissions error on a mkdir during the Cleaning up... segment of the install. And while all the files may have been installed, I suppose it wasn't completely configged.
sudo pip install django is no better because the django install now has su permissions which helps with the mkdir problem above, while potentially creating other problems.
After both installs, the python console import django would error.
The fix: the virtual env dirs need to be owned by the user that invoked the virtual envs.
sudo chown -R youruser:youruser /opt/myenv
In my case, puppet created the venv and dirs as the root user.
I am using a virtualenv. I have fabric installed, with pip. But a pip freeze does not give any hint about that. The package is there, in my virtualenv, but pip is silent about it. Why could that be? Any way to debug this?
I just tried this myself:
create a virtualenv in to the "env" directory:
$virtualenv2.7 --distribute env
New python executable in env/bin/python
Installing distribute....done.
Installing pip................done.
next, activate the virtual environment:
$source env/bin/activate
the prompt changed. now install fabric:
(env)$pip install fabric
Downloading/unpacking fabric
Downloading Fabric-1.6.1.tar.gz (216Kb): 216Kb downloaded
Running setup.py egg_info for package fabric
...
Successfully installed fabric paramiko pycrypto
Cleaning up...
And pip freeze shows the correct result:
(env)$pip freeze
Fabric==1.6.1
distribute==0.6.27
paramiko==1.10.1
pycrypto==2.6
wsgiref==0.1.2
Maybe you forgot to activate the virtual environment? On a *nix console type which pip to find out.
You can try using the --all flag, like this:
pip freeze --all > requirements.txt
Although your problem was specifically due to a typo, to help other users:
pip freeze doesn't show the dependencies that pip depends on. If you want to obtain all packages you can use pip freeze --all or pip list.
If you have redirected all the pre-installed packages in a file named pip-requirements.txt then it is pretty simple to fix the above issue.
1) Delete your virtualenv folder or create new one (I am giving it a name as venv)
rm -rf venv && virtualenv venv
2) Install all the requirements/dependencies from the pip-requirements.txt
pip install -r pip-requirements.txt
3) Now you can check the installed packages for your Django application
pip freeze
4) If you had forgotten to update your requirements file(pip-requirements.txt), then install fabric again (Optional Step)
Note: After installing any dependency for your Django app, always update the requirements in any file as follows (make sure your virtualenv is activated)
pip freeze > pip requirements.txt
That's it.
Adding my fix in addition of above fix also ,
I was also facing the same issue on windows,even after activating the virtualenv too pip freeze was not giving me all list of installed packages. So i upgraded my pip with python -m pip install --upgrade pip command and then used pip freeze.
This time it worked and gave me all list of installed packages.
This might be stupid but I have got the same problem. I solved it by refreshing vs code file directory (inside vscode there is a reload button). :)
If none of the above answers are working for you.
As with me you might have problem in you venv and pip configuration.
Go inside your venv/bin and open pip and see the 2nd line as:
'''exec' "path/to/yourvenv/bin/python3" "$0" "$#"
See if this line is correctly pointing inside your venv or not
For example in my case.
I initially named my virtual environment as venv1
and later just renamed it to venv2.
In doing so my pip file 2nd line had: '''exec' "venv1/bin/python3" "$0" "$#"
which to work properly should have: '''exec' "venv2/bin/python3" "$0" "$#" notice "venv2" not "venv1" since venv1 in now renamed to venv2.
Due to this python was looking inside pip of venv2 and throwing error or not working as desired.
For those who added Python modules via PyCharm IDE, after generating a virtual environment from the command prompt, good luck! You will need to rebuild the requirements.txt file manually with the ones missing by first running pip3 freeze and adding what is missing from PyCharm.
I highly suggest switching to Visual Studio Code.