Cannot resolve module 'imaplib2' has no attribute 'IMAP4_SSL' - python

There are many questions regarding imaplib2 has no attribute IMAP4_SSL but none of the solutions in the other questions worked for me.
I have a python script that's been running without issue for years on AWS, but after I upgraded to Ubuntu 20.04.2 LTS, it started throwing the error about IMAP4_SSL. After looking into the issue, I believe it may have something to do with the mappings in Python, but it all looks correct to me. I also checked that there are no other Python versions installed that could cause a conflict.
Here are the paths for Python and the imaplib2 package
$ which python3
/usr/bin/python3
$ python3 --version
Python 3.8.5
$ whereis python3
'/usr/bin/python3.8-config'
'/usr/bin/python3'
'/usr/bin/python3.8'
'/usr/lib/python3'
'/usr/lib/python3.8'
'/usr/lib/python3.9'
'/etc/python3'
'/etc/python3.8'
'/usr/local/lib/python3.8'
'/usr/include/python3.8'
'/usr/share/python3'
'/usr/share/man/man1/python3.1.gz'
$ sys.path
''
'/usr/lib/python38.zip'
'/usr/lib/python3.8'
'/usr/lib/python3.8/lib-dynload'
'/home/ubuntu/.local/lib/python3.8/site-packages'
'/usr/local/lib/python3.8/dist-packages'
'/usr/lib/python3/dist-packages'
> import imaplib2
> imaplib2.__file__
'/home/ubuntu/.local/lib/python3.8/site-packages/imaplib2/__init__.py'

The problem is likely that you are using an outdated version of imaplib2 from PyPI. You can install a newer version on Ubuntu using the following command.
sudo apt install python3-imaplib2
(Alternatively, if you are not on Ubuntu, you can always install from source.)
Depending on your PYTHONPATH and the location where you installed the old version of imaplib2, you may also need to uninstall the old version using pip before import imaplib2 will import the right version.
python3 -m pip uninstall imaplib2

Related

Using VSCode on Mac, can't get Python 3.8 to run, only Python 2.7.16

I just downloaded VSCode on my Mac as well as Python and pip. I downloaded the latest version of Python via VS Microsoft extensions and installed pip using Python -m ensurepip --upgrade. I have python 2.7.16 on my Mac as well, I believe from my OS (it's not something I downloaded). I am trying to use pip and Python in VSCode. I have my interpreter in my virtual environment set to Python 3.8.2, and I created the virtual environment using "python3 -m venv .venv". However, every time I check the python version (with Python --version) in the terminal, it returns Python 2.7.16. I check the pip version, and there is no command found. When I check the pip3 version, I get back the following warning:
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
You can invoke Python with '-m pip' instead of running pip directly to avoid this problem.
pip 21.3.1 from /Users/username/Library/Python/3.8/lib/python/site-packages/pip (python 3.8)
Whenever I run Python -m pip or Python -m pip3, it returns "no module named pip".
When installing Python, I got a warning that Python 3 was not on the PATH, but it did not fix the issue when I tried to add the path. Unfortunately, I don't have much experience adding a path so I may have done that incorrectly.
Finally, I installed streamlit via pip3. It resulted in the following message but seemed to install. However, it could not be resolved when I tried to import streamlit in my code.
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
You can invoke Python with '-m pip' instead of running pip directly to avoid this problem.
Defaulting to user installation because normal site packages is not writeable
How do I add Python 3 to my PATH and ensure that VSCode runs Python 3 instead of 2?
Simply always refer 'python3' rather than 'python'.
python3 --version
python3 -m pip install blahblahblah

Python version on RPI 3.8.2 but default is 3.5.3 issues

I have installed version 3.8.2 on my RPI because i previously had versions 2.7 and 3.5.3 installed. When I try to install stuff like ipython i get this: ERROR: Package 'ipython' requires a different Python: 3.5.3 not in '>=3.6'.
Tried various stuff and nothing works. Modified paths, installed 3.8.2 multiple times, tried to remove 3.5.3. Nothing works. It appareas the base version is 3.5 but I need it to be 3.8.2 and no idea how to do that.
Also when i try to run a python file I get the error that it cannot find numpy, and I have numpy installed in all versions.
Struggled for over a day to fix it and nothing. Can anybody help?
versions of python on RPI
Python is a requirement of Debian, hence uninstalling the version present by default is not recommended (it basically breaks the system).
If you need to run python 3.8 you should install it manually and call it with
python3.8 what_you_need.py
Also for pip:
python3.8 -m pip install what_you_need
I also suggest you to have a look to virtual environments:
https://docs.python.org/3/library/venv.html
https://github.com/pypa/pipenv

Command 'pytest' not found even though I moved pytest.py to /bin [duplicate]

There are already two posts on stack overflow on this topic; however, none of them have resolved or addressed my specific situation.
I have installed pytest via pip install pytest. I am able to import the library in Python as well.
The problem is that when I try to use the py.test command in Terminal, I get py.test: command not found.
Does anyone have any insight as to why I am not able to use the command in the terminal?
EDIT: It even shows up as an installed package:
$ pip list
cycler (0.9.0)
matplotlib (1.5.1)
numpy (1.10.1)
pip (8.1.0)
py (1.4.31)
pyparsing (2.0.7)
pytest (2.9.0)
python-dateutil (2.4.2)
pytz (2015.7)
scipy (0.17.0)
setuptools (7.0)
six (1.10.0)
tensorflow (0.5.0)
vboxapi (1.0)
wheel (0.26.0)
using python -m pytest will work for you.
Or if you using virtual environment and installed pytest on virtualenv you should then run py.test alongside your virtual environment.
Check this website can be useful:http://pythontesting.net/framework/pytest/pytest-introduction/
I already had the latest version of pytest on macOS with Homebrew-installed Python 2.7 and this fixed it:
pip uninstall pytest
pip install pytest
Are you on a mac with homebrew by any chance?
I had the same issue and it basically came down to permissions/conflict with the mac os base installed python. pip install would not install or link stuff into /usr/local/bin (it happened with both virtualenv and pytest).
I uninstalled python 2.7 completely with homebrew (brew uninstall python).
Next, I reinstalled python with homebrew to fix pip (it was not a symlink in /usr/local/bin/pip where it should have been linked to Cellar) -- brew install python
Then I uninstalled pip with sudo -- sudo python -m pip uninstall pip to remove the pip owned by root
Now I uninstalled and reinstalled python with homebrew again to reinstall pip with the correct permissions brew uninstall python && brew install python
Next I fixed the python symlinks brew link python
Finally, pip install pytest worked! (and so did pip install virtualenv)
I found the information in the chosen answer from this post very helpful:
https://superuser.com/questions/915810/pip-not-working-on-hombrew-python-2-7-install.
If you're not on a mac, sorry for the noise...
I may be late, but while exploring this I noticed that this can be because the Scripts folder for python is not present in the PATH.
For me this is my scripts folder:
C:\Python38\Scripts\
If the path is a problem then running pip install pytest should actually you give you the warning with the path it was added to.
This should be present in the path. If on windows, edit the environment variables and this location to the PATH.
For me the path was incorrect because of an improper installation of python
I had the same issue. I had pytest v2.8.3 installed and the binary was on my path but under the name py.test. Upgrading to v3.0.3 added the regular pytest executable to the path.
I had the same problem. I have changed the Python installed folder permission to full access. And then uninstalled the pytest and installed again.
pip uninstall pytest
In my case, I had a similar issue in ubuntu 20.04. The below solution worked for me.
Cause: Shell remembers the previous version or previously used Path, hence we need to force the shell to 'forget' the old location - with -r
hash -r pytest
Then execute the tests it should work fine.
For MAC users:
Download python universal installer for mac:
https://www.python.org/ftp/python/3.10.5/python-3.10.5-macos11.pkg
Then try to install pytest module in terminal using this command:
pip install pytest
Hope this will fix the issue. Thanks!
use the command, pip install -U pytest and install it in your cmd prompt, it will solve the issueenter image description here
I used macbook air m2, and the way I deal with this problem is:
Command in terminal in macbook:
which pytest
/opt/anaconda3/bin/pytest -> my terminal shows this
Then you got the path of pytest, in the "Command", before "pytest", add its path and following with the path of python file you wanna test.
/opt/anaconda3/bin/pytest /Users/cindyng/Desktop/Testing.py
Done, and if you cannot find the path of python in macbook, "which python" also helps, and you can put it in "Home" and "Custom Python Builder".
Hope that helps, good luck!
I Fixed this issue via below steps.
1.First uninstall existing pytest.
2.Check python version.
3.then verify pytest version is supported with python version or not via github issue tracker.
4. via sudo install pytest
sudo pip install pytest
5. verify pytest version and insatlled correctly or not.
pip list
pytest --version
6.run any test using pytest test_abc.py
I encounter the same problem, python -m pytest works for me.

Does the python.org installer of python come with pip, and how do I use it?

I can download python 2.7.12 from python.org, and all python versions from 2.7.9 onwards are supposed to come with pip, but after installing it, using pip in the terminal does not work.
I am on macOS.
Have I installed pip, and if I have, how do I use it?
Here you have informations about pip:
https://packaging.python.org/installing/
normally python from python.org come with pip, maybe you should just update...
to update from terminal:
pip install -U pip setuptools
After when you need to install package, for example numpy, just do in a terminal:
pip install numpy
more informations here :
https://pip.pypa.io/en/stable/reference/pip_install/
you can also use conda install from anaconda as an alternative of pip :
http://conda.pydata.org/docs/get-started.html
Multiple instances of Python can coexist on your machine. Thus you could have installed Python 2.7.12 yet, when you call Python from terminal, you may be calling an older version.
To know which version you are using, type which python in terminal and look at its path. Then from Python in terminal, type
import sys
print(sys.version)
to get the exact version.
As Dadep says, I would recommend using conda to isolate your invironments if you have to play with multiple Python interpreters. Further conda simplifies 3rd party package installation process beyond doubt.

Why 'python3 -m venv myenv' installs older version of pip into myenv than any version of pip I can find anywhere on the system?

This is not causing me any problem that I can't solve by activating the virtual environment and running pip install -U pip, but I always wonder where the older version of pip is coming from.
I'm using OS X 10.7.5. When I create a virtual environment using either pyvenv-3.4 myenv or python3 -m venv myenv, the version of pip that is installed inside the virtual environment is 6.0.8, but I have upgraded my global pip to 6.1.1.
Here is a terminal session demonstrating what I mean:
$ python3 -m venv myenv
$ myenv/bin/pip -V
pip 6.0.8 from /Users/dust/Desktop/myenv/lib/python3.4/site-packages (python 3.4)
Here is what I would like to occur:
$ source myenv/bin/activate
(myenv)$ pip -V
UPDATED SYSTEM VERSION HERE WOULD BE NICE
I can't find a pip 6.0.8 anywhere else, other than what is created inside virtual environments.
Here are the outputs of various commands that I have use to try and figure this out:
$ which pip
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip
$ which pip3
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
$ pip -V
pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
$ pip3 -V
pip 6.1.1 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)
I even tried using find:
$ find / -type f -name pip 2>&1 | awk '! /^f.*$/'
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip
/usr/local/bin/pip
$ find / -type f -name pip3 2>&1 | awk '! /^f.*$/'
/Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
I thought maybe that the /usr/local/bin/pip might have been the culprit, but no:
$ /usr/local/bin/pip -V
pip 6.1.1 from /Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg (python 2.7)
Hmm. Perhaps the OS X python has it?
$ /usr/bin/python
>>> import pip
>>> pip.__version__
'6.1.1'
6.1.1 is reported no matter which distribution of python I ask, whether it be OS X's 2.7.1, python.org's 2.7.9, or python.org's 3.4.3.
Is it possible (or advisable) to update the version of pip that gets put into a virtual environment?
I face the same issue, running OSX 10.10.2 and python 3.4.2. Most recently I created a virtual environment in a debian wheezy machine with python 3.4.3 and also ended up with an older version of pip than available. had to upgrade pip.
I've been upgrading pip within the virtual environment to 6.1.1 from 6.0.8 manually, because I'm o.c.d about software library versions that way - and yes, I am upgrading my python 3 version to 3.4.3 right now. Anyway, my system's python3-pip is the latest version 6.1.1, so I've also wondered why pyvenv creates a new virtual environment and loads it with old pip.
I haven't noticed anything bad happen in any of the virtual environments due to upgrading pip, (but on the flip side, I haven't noticed anything good either) Apparently the new pip is faster -- didn't notice, and outputs less junk on successful installs because user's don't care -- also didn't notice, probably because i'm one of those that don't care, and also comes with a state-of-the art coffee machine capable of latte art to boot!!! -- still waiting on sudo pip install latte to finish :(
So, to answer your question, it is definitely possible, and probably advisable to upgrade, because apparently the new pip fixes some bugs and goes faster, but I guess the speed up isn't that major, and the bug fixes don't affect all that many people (I've never faced a bug with my usage of the old pip).
You can link to system site-packages using the flag --system-site-packages when you create a new virtual environment, like this
pyvenv myenv --system-site-packages
This will link to your system wide version of pip, and would remove the annoyance that is manually upgrading pip on every virtual environment, but if you do this, then is your virtual environment all that virtual?
update: following my rant above, I went into the venv package's source to dig. pip is set up by a method called _setup_pip in the file __init__.py, line 248
def _setup_pip(self, context):
"""Installs or upgrades pip in a virtual environment"""
# We run ensurepip in isolated mode to avoid side effects from
# environment vars, the current directory and anything else
# intended for the global Python environment
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
'--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
So, venv seems to be calling ensurepip from the shell using the subprocess module.
One more minute of google-fu gave me this from the documentation for ensurepip.
ensurepip.version()
Returns a string specifying the bundled version of pip that will be installed when bootstrapping an environment.
So, from the command line, the following code:
$ python3 -c 'import ensurepip; print(ensurepip.version())'
6.0.8
displays my current pip that will be bootstrapped with ensurepip.
I guess we're stuck with the old version of pip for every new install until ensurepip gets upgraded, as I can't find a way to upgrade the version of pip that comes with ensurepip
Newer
If you want to "hotpatch" your installed python, just modify the versions listed in ensurepip/__init__.py and replace the two files in ensurepip/_bundled. You can find this location by running find * | grep ensurepip from the directory where python is installed. On macOS with Homebrew, this is the location: /usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ensurepip
You will also want to delete the ensurepip/__pycache__ directory that contains the .pyc files.
My older, build-time fix:
You are able to update the bundled versions of pip and setuptools by patching Python before building it from source. The following patch will update the bundled versions of pip and setuptools to the current available today. You will want to invoke configure with the following option: --with-ensurepip=upgrade
Those whl files are downloaded from PYPI here:
https://pypi.org/project/pip/#files
https://pypi.org/project/setuptools/#files
diff -ru Python-3.7.1/Lib/ensurepip/__init__.py Python-3.7.1.new/Lib/ensurepip/__init__.py
--- Python-3.7.1/Lib/ensurepip/__init__.py 2018-10-20 06:04:19.000000000 +0000
+++ Python-3.7.1.new/Lib/ensurepip/__init__.py 2018-11-27 02:36:19.301655008 +0000
## -8,9 +8,9 ##
__all__ = ["version", "bootstrap"]
-_SETUPTOOLS_VERSION = "39.0.1"
+_SETUPTOOLS_VERSION = "40.6.2"
-_PIP_VERSION = "10.0.1"
+_PIP_VERSION = "18.1"
_PROJECTS = [
("setuptools", _SETUPTOOLS_VERSION),
Only in Python-3.7.1/Lib/ensurepip/_bundled: pip-10.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: pip-18.1-py2.py3-none-any.whl
Only in Python-3.7.1/Lib/ensurepip/_bundled: setuptools-39.0.1-py2.py3-none-any.whl
Only in Python-3.7.1.new/Lib/ensurepip/_bundled: setuptools-40.6.2-py2.py3-none-any.whl
If you're lucky, you can bump it up with a Python patch.
Is your Python outdated? This is using macports, but any of brew, apt should do.
port outdated
python36 3.6.9 < 3.6.10
Let's fix that:
sudo port upgrade python36
And now... drumroll, in my newly-recreated venv, it seems that the Python patch has brought along the latest pip.
pip --version shows 20.0.2, it was at 18 previously and that's precisely what it was complaining about in the venv.
This is after spending a ludicrously long amount of time trying to figure out how to update my user-level Python 3.6 version of pip, on a mac where, of course, if you quit the virtualenv you are at Python 2.7 and where Python 3.6, installed through macports, doesn't seem to have a pip command to work with, until you build a venv.
From looking at /opt/local/Library/Frameworks/Python.framework/Versions/3.6, where macports puts things, I suspect Utknonos is on to something, so +1.
There really should be something a bit more obvious, like a pip-system-selfupdate command or a pip36 that comes along with python36. Especially considering that a search of pip upgrade will return tons of hits on anything but this subject.
Note: after my update, even though Python has been updated
(venv)$ pwd
/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6
(venv)$ grep PIP_VERSION ensurepip/__init__.py
_PIP_VERSION = "18.1"
(venv) $python --version
Python 3.6.10
(venv) $pip --version
pip 20.0.2 from .../venv/lib/python3.6/site-packages/pip (python 3.6)

Categories