Python 2.7 : LookupError: unknown encoding: cp65001 [duplicate] - python

This question already has answers here:
Windows cmd encoding change causes Python crash
(10 answers)
Closed 7 years ago.
I have installed python 2(64 bit), on windows 8.1 (64 bit) and wanted to know pip version and for that I fired pip --version but it is giving error.
C:\Users\ADMIN>pip --version
Traceback (most recent call last):
File "c:\dev\python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\dev\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\dev\Python27\Scripts\pip.exe\__main__.py", line 5, in <module>
File "c:\dev\python27\lib\site-packages\pip\__init__.py", line 15, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "c:\dev\python27\lib\site-packages\pip\vcs\mercurial.py", line 10, in <module>
from pip.download import path_to_url
File "c:\dev\python27\lib\site-packages\pip\download.py", line 35, in <module>
from pip.utils.ui import DownloadProgressBar, DownloadProgressSpinner
File "c:\dev\python27\lib\site-packages\pip\utils\ui.py", line 51, in <module>
_BaseBar = _select_progress_class(IncrementalBar, Bar)
File "c:\dev\python27\lib\site-packages\pip\utils\ui.py", line 44, in _select_progress_class
six.text_type().join(characters).encode(encoding)
LookupError: unknown encoding: cp65001
Note : The same command works fine for python 3. I have uninstalled both and installed again but still no success.

The error means that Unicode characters that your script are trying to print can't be represented using the current console character encoding.
Also try to run set PYTHONIOENCODING=UTF-8 after execute pip --version without reloading terminal if everything going well add PYTHONIOENCODING as env variable with value UTF-8. See How to set the path and environment variables in Windows article to get info how to add Windows variable.
NOTE: For PowerShell use $env:PYTHONIOENCODING = "UTF-8"
Also you can try to install win-unicode-console with pip:
pip install win-unicode-console
Then reload your terminal and try to execute pip --version
However you can follow suggestions from Windows cmd encoding change causes Python crash answer because you have same problem.

First of all you need to upgrade your pip.
You can do this in Windows by:
python -m pip install -U pip
Then go manually to your script folder, enter command line from that folder (you can do this by clicking shift + right mouse button -> Open console window), and then you should use one of this commands.
pip -V
pip --version
This will result in
pip 7.1.2 from c:\python27\lib\site-packages (python 2.7)
If you still have trouble, you can try to remove your current Python PATH, and add a new one to Python 2.7.

Related

Question about installing packages on Python 2.7

I am new to python and I am really confused about lot of things. I am developing my project mainly in PHP but as my host is unable to provide me access to v8js I have to use python to accomplish my tasks.
What I don't understand is:
On my server is running python 2.7, and If I use pip to install latest version of some package, will it install version that will work with python 2.7 or it will install it even if it is meant to run only in python 3?
I am trying to install and use https://pypi.org/project/cfscrape/ but I can't force it to work, but when try it on Repl.it it is working without any problem.
So I guess that something is not right on my server but don't know what, as I just used pip install cfscrape and it intalled that package.
Edit:
Do you think that this error could be result of using python 2 or this is not related to python version in use? All depadances are intalled
Traceback (most recent call last):
File "/usr/lib64/python2.7/runpy.py", line 151, in _run_module_as_main
mod_name, loader, code, fname = _get_module_details(mod_name)
File "/usr/lib64/python2.7/runpy.py", line 101, in _get_module_details
loader = get_loader(mod_name)
File "/usr/lib64/python2.7/pkgutil.py", line 464, in get_loader
return find_loader(fullname)
File "/usr/lib64/python2.7/pkgutil.py", line 474, in find_loader
for importer in iter_importers(fullname):
File "/usr/lib64/python2.7/pkgutil.py", line 430, in iter_importers
import(pkg)
File "main.py", line 3, in
test = scraper.get("https://www.klix.ba/koronavirus-u-bih").content
File "/home/admin/.local/lib/python2.7/site-packages/requests/sessions.py", line 543, in get
return self.request('GET', url, **kwargs)
File "/home/admin/.local/lib/python2.7/site-packages/cfscrape/init.py", line 129, in request
resp = self.solve_cf_challenge(resp, **kwargs)
File "/home/admin/.local/lib/python2.7/site-packages/cfscrape/init.py", line 204, in solve_cf_challenge
answer, delay = self.solve_challenge(body, domain)
File "/home/admin/.local/lib/python2.7/site-packages/cfscrape/init.py", line 292, in solve_challenge
% BUG_REPORT
ValueError: Unable to identify Cloudflare IUAM Javascript on website. Cloudflare may have changed their technique, or there may be a bug in the script.
On Repl.it it is working and on my server it is not
Most Python developers install more than one version of Python. This may be either due to the fact that the OS includes an old Python distribution or the developer himself/herself works on projects having different Python versions.
Running the pip executable directly with more than one Python distribution on a system is unpredictable and ambiguous. You can get some insight into what pip is actually executing using which command in Linux:
❯❯❯ which pip
/home/abhishek/software/miniconda2/envs/quantum/bin/pip
❯❯❯ which pip2
/home/abhishek/.local/bin/pip2
❯❯❯ which pip3
/home/abhishek/software/miniconda2/envs/quantum/bin/pip3
A better way to use pip is to explicitly specify the Python executable you want to use. This will make sure pip will use the python version you specified
❯❯❯ python -m pip # will use whatever python version this alias points to
❯❯❯ python2 -m pip
❯❯❯ python3 -m pip
PS: Python 2 is already depreciated. It is strongly recommended to use Python 3 for all use-cases.

Cant install pygame [duplicate]

This question already has answers here:
Unable to install pygame on Python via pip (Windows 10)
(6 answers)
Closed 2 years ago.
So I've got a bit of an issue with python and/or pip...
Python, when asked to download pygame in CMD with py -m pip install -U pygame --user, gives me this error:
WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
Using WINDOWS configuration...
Download prebuilts to "prebuilt_downloads" and copy to "./prebuilt-x64"? [Y/n]Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\gary\AppData\Local\Temp\pip-install-1zalja6w\pygame\setup.py", line 194, in <module>
buildconfig.config.main(AUTO_CONFIG)
File "C:\Users\gary\AppData\Local\Temp\pip-install-1zalja6w\pygame\buildconfig\config.py", line 210, in main
deps = CFG.main(**kwds)
File "C:\Users\gary\AppData\Local\Temp\pip-install-1zalja6w\pygame\buildconfig\config_win.py", line 576, in main
and download_win_prebuilt.ask(**download_kwargs):
File "C:\Users\gary\AppData\Local\Temp\pip-install-1zalja6w\pygame\buildconfig\download_win_prebuilt.py", line 302, in ask
reply = raw_input(
EOFError: EOF when reading a line
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
It seems to download the files just fine but does NOT like to continue after that. I'm running python 3.8. And yes, Python is installed in the PATH variable.
I feel like this could be a really stupid issue and I've just overlooked something but I cant seem to find anything that helps.
-----edit-----
I've worked out a bit of a solution - just download and use python 3.7 instead of 3.8 and that seems to work better.
Assuming you're using an IDE with the ability to automatically install imports (PyCharm for example) you can just set your IDE's current interpreter to python 3.7.4 and use it to install pygame instead of using pip. The problem has something to do with the pygame install not working with python 3.8 so either revert your interpreter, role back to python 3.7 or wait for pygame to get updated

ipython3 does not work in the terminal with python3.7

I recently upgraded from Python3.6 to Python3.7. Since I have upgraded, when I type in ipython3 in the terminal I get an error:
~$ ipython3
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3/dist-packages/IPython/__init__.py", line 48, in <module>
from .core.application import Application
File "/usr/lib/python3/dist-packages/IPython/core/application.py", line 25, in <module>
from IPython.core import release, crashhandler
File "/usr/lib/python3/dist-packages/IPython/core/crashhandler.py", line 28, in <module>
from IPython.core import ultratb
File "/usr/lib/python3/dist-packages/IPython/core/ultratb.py", line 124, in <module>
from IPython.utils import path as util_path
File "/usr/lib/python3/dist-packages/IPython/utils/path.py", line 18, in <module>
from IPython.utils.process import system
File "/usr/lib/python3/dist-packages/IPython/utils/process.py", line 19, in <module>
from ._process_posix import system, getoutput, arg_split, check_pid
File "/usr/lib/python3/dist-packages/IPython/utils/_process_posix.py", line 24, in <module>
import pexpect
File "/usr/lib/python3/dist-packages/pexpect/__init__.py", line 75, in <module>
from .pty_spawn import spawn, spawnu
File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 14, in <module>
from .spawnbase import SpawnBase
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 224
def expect(self, pattern, timeout=-1, searchwindowsize=-1, async=False):
^
SyntaxError: invalid syntax
Furthermore I have noticed that my jupyter-notebook does not seem to work with the python3 kernel now as well (I get the kernel dead error).
NOTEs:
ipython and jupyter-notebook are working with fine when I use them with Python2.
I am using Ubuntu 18.04 (Bionic Beaver) although I don't think this is relevant
async is a reserved keyword in python3.7 and an old version of pexpect is using async as variable. The solution is to upgrade pexpect.
For me, it was conflicting with apt-get installed python3, so I had to first uninstall it/them:
sudo apt-get remove python-pexpect python3-pexpect
And then
sudo pip3.7 install --upgrade pexpect
UPDATE Please update your installed packages.
This error for pexpect has been reported and closed already issue
In Python 3.7, async and await are now reserved keywords. This is what is breaking some of your installed packages.
If you do not need the new features in 3.7, roll back to 3.6 and wait until your packages are updated to support the new syntax in 3.7
What's New in Python 3.7
I just solve this problem by upgrading pexpect manually.
Download pexpect4.6 source code from https://github.com/pexpect/pexpect/releases/tag/4.6
Extract the source code, get into the folder and install the pexpect by:
sudo python3.7 setup.py install
As others have noted, having the current version of the pexpect package should solve this. In my case, the python3-pexpect package that I had installed through apt was stuck on an old version and I first had to remove it. Then installing the current pexpect package through pip3 fixed the problem.
This appears to be an issue related to async being keyword in Python 3.7
As given here, updating pipenv might be the solution
async is a reserved word in Python 3.7
You can edit the packages yourself if you feel comfortable doing it.
Here is a shell command that does just that:
sed "s/async/_async/g" "/usr/lib/python3/dist-packages/pexpect/spawnbase.py" > tmp.txt && cat tmp.txt > "/usr/lib/python3/dist-packages/pexpect/spawnbase.py" && rm tmp.txt
Otherwise, you might want to use Python 3.6 while waiting for an update.

Error with TensorFlow MNIST [duplicate]

Since updating from Homebrew Python 2.7.11 (from 2.7.10) I'm suddenly unable to test register my package on PyPi from the PyCharm IDE console.
Running (as an "External Tool")
python -B setup.py register -r pypitest
I now get
Traceback (most recent call last):
File "setup.py", line 22, in <module>
from setuptools import setup
File "/usr/local/lib/python2.7/site-packages/setuptools/__init__.py", line 12, in <module>
from setuptools.extension import Extension
File "/usr/local/lib/python2.7/site-packages/setuptools/extension.py", line 8, in <module>
from .dist import _get_unpatched
File "/usr/local/lib/python2.7/site-packages/setuptools/dist.py", line 16, in <module>
from setuptools.depends import Require
File "/usr/local/lib/python2.7/site-packages/setuptools/depends.py", line 6, in <module>
from setuptools import compat
File "/usr/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in <module>
import httplib
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 80, in <module>
import mimetools
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Process finished with exit code 1
I'm not sure how to proceed. I only get this issue if I execute from within my IDE's console. If I do it directly at the system command line (Terminal on OS X) I have no problems.
OS X 10.11.3; Homebrew Python 2.7.11; PyCharm 5.0.3
tl;dr: Fix this issue by doing one of the following:
type hash -r python, OR
log out and log in.
EDIT: An answer to my related question makes it clear what's happening here. When you install a new version of python, you may need to run hash -r python to tell bash to reset the "cached" location to the python executable.
In my case, I was typing python, which was on my $PATH at /usr/local/bin/python. But bash was still using the old cache location /usr/bin/python. So, the old executable was called, but the new path was provided to python in sys.argv[0]. This means that the old executable was running, but the new sys.executable value caused all the wrong modules to get loaded (including the io module).
I'm having the same problem. I installed python 2.7.11 via an installer from Python.org. Strangely, the issue seems to be related to some subtle difference between how OSX launches python when I invoke it from the shell using the full path vs. using just the word python.
So, for me, this works (invoking python via the full path /usr/local/bin/python):
$ which python
/usr/local/bin/python
$ /usr/local/bin/python -c "import io"
$
... but this doesn't:
$ python -c "import io"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
import _io
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
So, as a workaround, you can try doing the same thing.
Elsewhere, I've posted a separate question about this puzzling behavior. Maybe somehow merely calling python invokes some strange mix of the 2.7.11 executable with the 2.7.10 dylibs??
According to https://github.com/klen/python-mode/issues/634:
I had the same issue, but successfully fixed. In my case I compiled
python and vim with homebrew, when PYTHON_PATH has been specified and
set to one of my dev environments, where I also had some libraries,
including io. Workaround was simple: open new terminal, make sure that
you do not have custom PYTHON_PATH, uninstall python, uninstall vim.
Reinstall both of them.
and
Problem solved.
Culprit is the update from python 2.7.10 to 2.7.11.
If you are using conda package control, simply run "conda install
python=2.7.10" will solve this problem.
This doesn't give the root cause though. Since this happens with _io, this looks like a bug in python 2.7.11 (unlikely, there would be a world-scale outcry and a prompt fix if it was) or some packaging bug or version mismatch specifically with the homebrew version (and maybe some related ones, too).
Try to import _io in the console and if it succeeds, check if it was loaded from the same path.
Reinstall python.
brew unlink python && brew reinstall python
Secure the path
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/
BACKUP and Change the order of "paths" file.
sudo nano /etc/paths
it seems, the order of paths, it is decisive to run python properly. In my case, the result was:
#sudo nano /etc/paths
/usr/bin
/usr/local/bin
/bin
/usr/sbin
/sbin
On my mac, path is like this.
$ which python
/usr/local/bin/python
Now I can run both:
$ /usr/local/bin/python -c "import io"
$ python -c "import io"
I had the same issue, it is successfully fixed by just replacing the _io.so file.
sudo find / -name _io.so
copy the path of the _io.so file which DOES NOT belong to python-2.7.11. For example, copy the path of _io.so which is under python-2.7.5:
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
Replace the /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so file with the _io.so that you just found.
This happened to me as well in MacVim. I solved it by making sure :python print(sys.path) is using system Python (e.g. /Library/Python/2.7/...)
Since I installed MacVim via Homebrew, I just did that by:
Spawn a new shell that had which python -> /usr/bin/python. For my case I needed to remove the pyenv line from my .bash_profile. If you installed Python via Homebrew you may want to brew unlink python first
brew reinstall macvim
If your problem is caused by anaconda, it is unnecessary to remove //anaconda directory.
Just open your ~/.bash_profile, find the line
export PATH="//anaconda/bin:$PATH
and comment it out, then restart your terminal session.
Another quick workaround if you don't mind sticking with Python 2.7.10 is to specify the path of the Python interpreter executable that will be used for the virtualenv. On OSX that path is usually /usr/bin/python:
virtualenv venv --python=/usr/bin/python
Can't add comment (?) so this just to share my exp., downgrade to 2.7.10 works fr me.
I got this error after a failed NLTK download, I needed to uninstall anaconda:
sudo rm -rf ~/anaconda
update PATH variable
This happened when I already had tried to create a venv in a folder, and mistakenly was trying to initialize a second one! So I just removed venv directory and re-ran the command. Very likely this is not the answer to this solution, but searching my error brought me here, so it may help some others who are stuck.
I solved this issue by removing the symbolic link that was in /usr/local/bin and copying the actual python binary, that was pointed to by said link, there.
I had the same issue when I tried to use PyCharm. Solved by setting "python interpreter" in project configuration to point to the python virtual env I wanted to use, which was an Anaconda env. Somehow the interpreter path was missing the "anaconda" portion of ~/.../anaconda/.../_io.so. No need to uninstall anaconda.

pip error: Bad interpreter when trying to run 'pip install ...' on OS X

I am trying to install a fresh version of virtualenv (there is some problem with the path that python has stored in sys.executable) and it turns out there is another problem that actually seems related.
When I try to run pip install virtualenv, I get this output:
-bash: /usr/local/Cellar/python/2.7.10_2/bin/pip: /usr/local/opt/python3/bin/python3.4:
bad interpreter: No such file or directory
Now my original point in reinstalling virtualenv is that I keep getting this error when I run virtualenv venv
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/virtualenv.py", line 2363, in <module>
main()
File "/Library/Python/2.7/site-packages/virtualenv.py", line 832, in main
symlink=options.symlink)
File "/Library/Python/2.7/site-packages/virtualenv.py", line 994, in create_environment
site_packages=site_packages, clear=clear, symlink=symlink))
File "/Library/Python/2.7/site-packages/virtualenv.py", line 1288, in install_python
shutil.copyfile(executable, py_executable)
File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/shutil.py", line 108, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/opt/python3/bin/python3.4'
As a sidenote that may be relevant, rather than using the default installed python3, I brew-installed a python3 with a brewed openssl (an application required it) and placed the path of the brewed python3 at the top of my /etc/paths.
I have tried to install and uninstall python multiple times without any success and am completely at a loss of what the problem could be. Any direction would be greatly appreciated.
EDIT
After #cel 's suggestions, it turned out that the head of my pip file (located at /usr/local/Cellar/python/2.7.10_2/bin/pip) was set to hardcode a python version as so #!/usr/local/opt/python3/bin/python3.4. I changed this to the output of which python which was /usr/local/Cellar/python/2.7.10_2/bin/python
This allowed me to succesfully create a virtualenv once again. Thanks!
I came across a similar problem when I used HomeBrew to upgrade my python from version 3.5.2 to version 3.6.0. HomeBrew updated the symlink /usr/local/bin/pip3 to /usr/local/Cellar/python3/3.6.0/bin/pip3 but my /usr/local/bin/pip was still using the old script and it was pointing to the python interpreter at /usr/local/opt/python3/bin/python3.5.
My course of action was as follows:
Unlinked /usr/local/bin/pip and updated it to point /usr/local/Cellar/python3/3.6.0/bin/pip3.
Created a new symlink /usr/local/bin/pip3.5 that pointed to /usr/local/Cellar/python3/3.5.2/bin/pip3 and updated the header of pip3.5 script to point to the python interpreter at /usr/local/Cellar/python3/3.5.2/bin/python3.5
Followed the same procedure for virtualenv.
P.S. Initially I didn't face this problem with virtualenv since I was using it with the -p option.

Categories

Resources