Seeing escape characters when pressing the arrow keys in python shell - python

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.
But after I ssh into another machine and start python there, I get sessions like:
>>> import os
>>> ^[[A
where the last character comes from arrow-up. Or, using arrow-left:
>>> impor^[[D
How can I fix this?
In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.

I've solved this issue by installing readline package:
pip install readline

On OS X, I have different problem.
When I using system python shell, the keys is no problem, but problem in virtualenv. I'd try to reinstall/upgrade virtualenv/readline and nothing fixed.
While I try to import readline in problem python shell, get this error message:
ImportError: dlopen(/Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so
Reason: image not found
Cause there is /usr/local/opt/readline/lib/libreadline.7.dylib but not libreadline.6.dylib, so I make a symbol link:
ln -s libreadline.7.dylib libreadline.6.dylib
Problem has been solved!

On OS X, Xcode updates sometimes break readline. Solution:
brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline
If the problem still persists, try to remove readline using pip and install it using easy_install:
pip3 uninstall readline
easy_install readline

Looks like readline is not enabled. Check if PYTHONSTARTUP variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.
Thanks to #chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html

On OS X, using python 3.5 and virtualenv
$ pip install gnureadline
In the interpreter do:
import gnureadline
Now arrow keys should work properly.
Additional information...
Note that as of Oct 1, 2015 - readline has been DEPRECATED (source https://github.com/ludwigschwardt/python-readline)
Use gnureadline instead (see: https://github.com/ludwigschwardt/python-gnureadline)
If I install readline instead of gnureadline using python 3.5, I receive errors after attempt to import in the interpreter:
>>> import readline
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so
Reason: image not found

I have run into this issue recently and after reading a lot about pip install readline (does not work for mac osx) and pip install gnureadline and not being satisfied, this is now my setup which enables using arrow keys in any python console:
install gnureadline using pip install gnureadline
now you can either do import gnureadline and arrow keys should work as expected. To make them work automatically follow the following steps:
create (or append to) file ~/.startup.py: import gnureadline
append to file ~/.bash_profile: export PYTHONSTARTUP=~/.startup.py
One thing that does not work, but did in my previous setup is: automatic import of gnureadline on pdb.set_trace(). If anyone has a good solution to this problem I would be grateful for a comment.

I had problems with shell history(tab/arrows commands) of Python 3.6.x on Ubuntu 16.04 LTS.
Python 3.6.x was installed from source.
What solved for me was install the module "gnureadline" as said by user12345, using this command line:
sudo pip3.6 install gnureadline
:)

install readline-devel package.
recompile python with readline module
Bingo!

Here are the steps which worked for me in ubuntu 12.04 for python 3.3.
1) open teminal and write sudo apt-get install libreadline-dev
2) download the source file of python 3.3.2 from http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz
3) extract it and navigate to the Python-3.3.2/ directory in a shell
4) execute the following command:
./configure
make
make test
sudo make install

Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:
brew unlink python
xcode-select --install
brew install python

On CentOS, I fix this by
yum install readline-devel
and then recompile python 3.4.
On OpenSUSE, I fix this by
pip3 install readline
following Valerio Crini's answer.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.

If you use Anaconda Python, you can fix this by running:
conda install readline
Worked for me!

readline module has been deprecated which will cause invalid pointer error in latest python versions when executing quit() or exit() in python shell.
pip install gnureadline instead

None of these answers worked for me on two different version of Ubuntu. What worked for me, but isn't a true fix, is wrapping my python code in a call to rlwrap (available in the ubuntu repositories):
rlwrap python mycode.py

I fixed this by doing the following:
yum install readline-devel
pip install readline
I encountered another error here:
gcc: readline/libreadline.a: No such file or directory
gcc: readline/libhistory.a: No such file or directory
I fixed this by installing patch:
yum install patch
After that I managed to run pip install readline successfully which solved the escape characters in my python shell.
FYI, I'm using RedHat

For those using conda, installing the readline package from conda-forge channel will fix the problem:
conda install -c conda-forge readline=6.2

Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?
From the man page:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.

On Mac OS X Mojave 10.14.6 with various historical installs via brew I solved this with:
brew reinstall python2
There is likely no magic bullet given everyone has a different install scenario. I tried the above as well so it may have been a combination of a few of the answers. Brew defaults to python3 so if you installed the python2 package it also needs to be reinstalled.

Have you tried using a different SSH client? Some SSH clients have special, built-in keymappings for different remote processes. I ran into this one a lot with emacs.
What client are you using? I'd recommend trying Putty and SecureCRT to compare their behavior.

How's your env variable $TERM set [a] when things work fine and [b] when they don't? Env settings are often the key to such problems.

Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.

I was trying build Python 2.7 on Ubuntu 14.0. You will need libreadline-dev. However, if you get it from apt-get, the current version is 6.3, which is incompatible with Python 2.7 (not sure about Python 3). For example, the data type "Function" and "CPPFunction", which were defined in previous versions of readline has been removed in 6.3, as reported here:
https://github.com/yyuu/pyenv/issues/126
That is to say you need to get the source code of an earlier version of readline. I installed libreadline 5.2 from apt-get for the library, and get the source code of 5.2 for the header files. Put them in /usr/include.
Finally the issue has been resolved.

On MacOsx, I fixed this by reinstalling readline
brew reinstall readline

In Unbuntu or Mint,
if you are using pyenv,
sudo apt install libreadline-dev
pyenv uninstall 3.8.8
pyenv install 3.8.8
Once installing libreadline-dev, you don't need to install pip install gnureadline on every python version.

you can switch from 'sh" to "bash" by
$ /sh/bash

Related

Installed python package with pip but can't import said package

I installed a python3 package called urllib3 using pip with the following command:
pip install urllib3
It seemed to be successful and when I type the command:
pip list
I get the following list which implies it is installed
But when I try to import in in the python console I get the following error:
ModuleNotFoundError: No module named 'urllib3
I am using Python 3.8.5 (64bit) with a VS Code virtual enviroment, I have tried uninstalling it and reinstalling I've also checked typos but I can't seem to find any.
try:
pip3 install urllib3
As it can be possible you have also installed python2 on your machine.
Also try restarting your IDE.
You're paths might be mixed up: python, python3 as well as pip and pip3 might not point to the places you're expecting.
Try to validate that which pip and which python point to the same python installation.
Alternatively, you can install urllib3 using python -m pip install urllib3 instead of pip install urllib3.
Lastly, it might be a VS Code issue. See if the information on selecting a Python interpreter are helpful to you.
The problem was with permissions for running scripts on the computer, I had to run the following command to enable permissions on my system:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
This enabled the current user (me) to actually activate the venv when I opened powershell (I think) and run the pip install inside the venv instead of outside.
Note that I had the package installed globally since the PowerShell was not inside the venv.
Thanks for the help
Resolved!

python 3.6.3: backspace, arrow keys not working properly [duplicate]

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.
But after I ssh into another machine and start python there, I get sessions like:
>>> import os
>>> ^[[A
where the last character comes from arrow-up. Or, using arrow-left:
>>> impor^[[D
How can I fix this?
In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.
I've solved this issue by installing readline package:
pip install readline
On OS X, I have different problem.
When I using system python shell, the keys is no problem, but problem in virtualenv. I'd try to reinstall/upgrade virtualenv/readline and nothing fixed.
While I try to import readline in problem python shell, get this error message:
ImportError: dlopen(/Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so
Reason: image not found
Cause there is /usr/local/opt/readline/lib/libreadline.7.dylib but not libreadline.6.dylib, so I make a symbol link:
ln -s libreadline.7.dylib libreadline.6.dylib
Problem has been solved!
On OS X, Xcode updates sometimes break readline. Solution:
brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline
If the problem still persists, try to remove readline using pip and install it using easy_install:
pip3 uninstall readline
easy_install readline
Looks like readline is not enabled. Check if PYTHONSTARTUP variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.
Thanks to #chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html
On OS X, using python 3.5 and virtualenv
$ pip install gnureadline
In the interpreter do:
import gnureadline
Now arrow keys should work properly.
Additional information...
Note that as of Oct 1, 2015 - readline has been DEPRECATED (source https://github.com/ludwigschwardt/python-readline)
Use gnureadline instead (see: https://github.com/ludwigschwardt/python-gnureadline)
If I install readline instead of gnureadline using python 3.5, I receive errors after attempt to import in the interpreter:
>>> import readline
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so
Reason: image not found
I have run into this issue recently and after reading a lot about pip install readline (does not work for mac osx) and pip install gnureadline and not being satisfied, this is now my setup which enables using arrow keys in any python console:
install gnureadline using pip install gnureadline
now you can either do import gnureadline and arrow keys should work as expected. To make them work automatically follow the following steps:
create (or append to) file ~/.startup.py: import gnureadline
append to file ~/.bash_profile: export PYTHONSTARTUP=~/.startup.py
One thing that does not work, but did in my previous setup is: automatic import of gnureadline on pdb.set_trace(). If anyone has a good solution to this problem I would be grateful for a comment.
I had problems with shell history(tab/arrows commands) of Python 3.6.x on Ubuntu 16.04 LTS.
Python 3.6.x was installed from source.
What solved for me was install the module "gnureadline" as said by user12345, using this command line:
sudo pip3.6 install gnureadline
:)
install readline-devel package.
recompile python with readline module
Bingo!
Here are the steps which worked for me in ubuntu 12.04 for python 3.3.
1) open teminal and write sudo apt-get install libreadline-dev
2) download the source file of python 3.3.2 from http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz
3) extract it and navigate to the Python-3.3.2/ directory in a shell
4) execute the following command:
./configure
make
make test
sudo make install
Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:
brew unlink python
xcode-select --install
brew install python
On CentOS, I fix this by
yum install readline-devel
and then recompile python 3.4.
On OpenSUSE, I fix this by
pip3 install readline
following Valerio Crini's answer.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.
If you use Anaconda Python, you can fix this by running:
conda install readline
Worked for me!
readline module has been deprecated which will cause invalid pointer error in latest python versions when executing quit() or exit() in python shell.
pip install gnureadline instead
None of these answers worked for me on two different version of Ubuntu. What worked for me, but isn't a true fix, is wrapping my python code in a call to rlwrap (available in the ubuntu repositories):
rlwrap python mycode.py
I fixed this by doing the following:
yum install readline-devel
pip install readline
I encountered another error here:
gcc: readline/libreadline.a: No such file or directory
gcc: readline/libhistory.a: No such file or directory
I fixed this by installing patch:
yum install patch
After that I managed to run pip install readline successfully which solved the escape characters in my python shell.
FYI, I'm using RedHat
For those using conda, installing the readline package from conda-forge channel will fix the problem:
conda install -c conda-forge readline=6.2
Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?
From the man page:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.
On Mac OS X Mojave 10.14.6 with various historical installs via brew I solved this with:
brew reinstall python2
There is likely no magic bullet given everyone has a different install scenario. I tried the above as well so it may have been a combination of a few of the answers. Brew defaults to python3 so if you installed the python2 package it also needs to be reinstalled.
Have you tried using a different SSH client? Some SSH clients have special, built-in keymappings for different remote processes. I ran into this one a lot with emacs.
What client are you using? I'd recommend trying Putty and SecureCRT to compare their behavior.
How's your env variable $TERM set [a] when things work fine and [b] when they don't? Env settings are often the key to such problems.
Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.
I was trying build Python 2.7 on Ubuntu 14.0. You will need libreadline-dev. However, if you get it from apt-get, the current version is 6.3, which is incompatible with Python 2.7 (not sure about Python 3). For example, the data type "Function" and "CPPFunction", which were defined in previous versions of readline has been removed in 6.3, as reported here:
https://github.com/yyuu/pyenv/issues/126
That is to say you need to get the source code of an earlier version of readline. I installed libreadline 5.2 from apt-get for the library, and get the source code of 5.2 for the header files. Put them in /usr/include.
Finally the issue has been resolved.
On MacOsx, I fixed this by reinstalling readline
brew reinstall readline
In Unbuntu or Mint,
if you are using pyenv,
sudo apt install libreadline-dev
pyenv uninstall 3.8.8
pyenv install 3.8.8
Once installing libreadline-dev, you don't need to install pip install gnureadline on every python version.
you can switch from 'sh" to "bash" by
$ /sh/bash

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

I am using Python 3.6. When I try to install "modules" using pip3, I face this issue:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available
For Windows 10
if you want use pip in normal cmd, not only in Anaconda prompt. you need add 3 environment paths.
like the followings:
D:\Anaconda3
D:\Anaconda3\Scripts
D:\Anaconda3\Library\bin
most people only add D:\Anaconda3\Scripts
MAC OS
I had the same problem on Mac OS(Mojave) and solved the problem as mentioned on this link - Openssl issue.
If you do not have Homebrew or don't know what is Homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Or if you already have Homebrew installed:
brew update && brew upgrade
brew uninstall --ignore-dependencies openssl; brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
Update:
Keep in mind, that I had to use --ignore-dependencies flag, because other packages installed that depend on OpenSSL.
Additional if the problem is caused after using pyenv, you can fix it by using:
brew reinstall python
For Debian users, the following may be of use:
sudo apt install libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev libtk8.6 libgdm-dev libdb4o-cil-dev libpcap-dev
Then cd to the folder with the Python 3.X library source code and run:
./configure
make
make install
I'm using Windows 10 and installed Miniconda 3 with Python 3.7.
I solved this error by following this https://github.com/conda/conda/issues/8273
Specifically, I copied the following files from C:\Users\MyUser\Miniconda3\Library\bin to C:\Users\MyUser\Miniconda3\DLLs:
libcrypto-1_1-x64.dll
libcrypto-1_1-x64.pdb
libssl-1_1-x64.dll
libssl-1_1-x64.pdb
For centos 7:
Install openssl:
sudo yum install openssl-devel
now goto python directory were we extracted the python tar,
run below commands
sudo ./configure
sudo make
sudo make install
This will fix the problem in centos...
For future Oracle Linux users trying to solve this, below is what worked for me.
First install missing libs:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
Then cd to your Python3.X library and run:
make
make install
macOS, pyenv
In case of your python being an pyenv installed one, where pyenv is installed with homebrew on macOS, there might me a newer version available which fixes this:
$ brew update && brew upgrade pyenv
Then reinstalling the python version:
$ pyenv install 3.7.2
pyenv: /Users/luckydonald/.pyenv/versions/3.7.2 already exists
continue with installation? (y/N)
Note, it is a bit dirty to overwrite the existing python install like that, but in my case it did work out. Probably cleaner to delete it and then recreate it properly.
For Windows 10,windows 7
If pip install is not working on CMD prompt, run it using Anaconda prompt - it works.
https://github.com/pypa/virtualenv/issues/1139
Worked for me.
sudo apt-get install libssl-dev
Use this to enable ssl for pip.
Let me know if someone encounters issues.
Encountered this issue while installing python 3.8 from source on ubuntu. The steps needed to install it successfully alongside the default python 3.7 are summarised below :
sudo apt -y install libssl-dev zlib1g-dev build-essential
wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
tar -xf Python-3.8.0.tgz
cd Python-3.8.0/
./configure --enable-optimizations
make
sudo make altinstall
The install instruction for zlib1g-dev and build-essential is redundant, as ubuntu desktop already has these, but was necessary for some of Amazon's EC2 instances. python 3.8.0 is the current release just now, but should be replaced with the latest available.
These instructions are best for keeping python 3.7 as the default for python3, and running python 3.8 in a virtual environment.
Similar to the above solution reinstall the python version with pyenv.
Somehow, I upgraded my openssl which broke the pyenv version python.
pyenv install 3.6.8
python-build: use openssl#1.1 from homebrew
python-build: use readline from homebrew
...
The first line says it relies on the homebrew openssl.
In my case, I reinstalled Python. It solved the problem.
brew reinstall python
For OpenSUSE in the same manner, but a few changes of listed above packages:
zypper install zlib-devel libopenssl-devel ncurses-devel sqlite3-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel
Then cd to Python sources dir and
make
make install
or
make
make altinstall
And perhaps
ln -s /usr/local/lib64/python3.6/lib-dynload/ /usr/local/lib/python3.6/lib-dynload
should be executed for OpenSUSE users. See Python 3.7 install not working on openSUSE Leap 42.3
Just try installing through Anaconda prompt
I ran into this issue with Visual Studio Code installing pylint from the VS Code prompt.
I was able to overcome the issue by opening the Anaconda installation directory and running
pip install pylint
Then VS Code was happy, but that did not fix the issue as running
& C:/Users/happy/Anaconda3/python.exe -m pip install -U pylint
pretty much gave the same error so it seems that VS Code is unable to access the python modules.
Note that VS Code picks up the first python env it see when installed, the bottom left of the screen indicates which env is being used. Clicking on that area allows to set the environment. So even if you ran the pip install for an environment VS Code could be looking at a different one.
Best approach was to make sure that VS code had the correct python environment selected and that same environment is in the system PATH (under System Properties --> Advanced --> Environmental Variables)
Under the Path Variable, Edit and browse to the specific Anaconda directory that you want VSCode to use and add to PATH, I needed to Add the following:
C:\Users\happy\Anaconda3\
C:\Users\happy\Anaconda3\Scripts\
C:\Users\happy\Anaconda3\Library\bin\
C:\Users\happy\Anaconda3\Library\mingw-w64\bin\
Your Anaconda installation directory may differ.
One note is that Windows does not have the PATH variable take effect until you restart the terminal. In this case close and re-op VS code. If using a Terminal or PS Shell then close and reopen and check Path to make sure it is included.
The problem probably comes from your installed openssl package version. That was the case for me and I fixed this issue just upgrading it. I'm on Mac OS, using brew :
brew upgrade openssl
If you installed python with brew, this should directly fix the issue with it, as python is dependent on openssl
Newest Python 3.8.4 or higher should able to support https protocol out of box. If you still have old python installation on your pc - either download & install python3 manually, or using Chocolatey:
If you don't have Chocolatey, install it - from here: https://chocolatey.org/docs/installation
You can just copy paste one command line liner and execute it from command prompt with elevated priviledges.
choco install python3
if you don't have python3 installed, or you you have it installed - then:
choco upgrade python3
Notice also that you can use also anaconda distribution, as it has built-in python with https support, but this rather ancient instructions, no need to follow them anymore.
Install anaconda, using command line:
choco install anaconda3
Set environment variables:
set PATH=C:\tools\Anaconda3\Scripts;C:\tools\Anaconda3;C:\tools\Anaconda3\Library\bin;%PATH%
and then run command which failed. In my case it was:
pip install conan
Anaconda uses separate python installation, and pip is also anaconda specific.
As Tokci said, it also works for Windows 7.
"Go with the mouse to the Windows Icon (lower left) and start typing "Anaconda". There should show up some matching entries. Select "Anaconda Prompt". A new command window, named "Anaconda Prompt" will open."
Then pip works.
The following also helped to import xgboost:
https://www.youtube.com/watch?v=05djBSOs1FA
If someone is using Arch Linux OS, I solved the TLS/SSL problem by running this:
sudo pacman -S openssl
Then I could use pip to install the package I needed:
pip install openpyxl
Go to Anaconda prompt and type (if you have python 3.x installed on your engine) :
py -m pip install pymysql
i was having the same issue and this solved my problem. later after doing this you can import pymysql in power shell or any other prompt.
The issue is due to OpenSSL package is missing on your PC.
If pip install openpyxl also gives error.
you can fix this by installing OpenSSL(Win64 OpenSSL v1.1.1g) from below site :
slproweb.com/products/Win32OpenSSL.html
Restart the IDE you are using, for changes to be in effect.
In Windows 10 SQL Server 19 the solution is known.
Copy the following files:
libssl-1_1-x64.dll
libcrypto-1_1-x64.dll
from the folder
C:\Program Files\Microsoft SQL Server\MSSSQL15.MSSQLSERVER\PYTHON_SERVICES\Library\bin
to the folder
C:\Program Files\Microsoft SQL Server\MSSSQL15.MSSQLSERVER\PYTHON_SERVICES\DLLs
Then open a new DOS command shell prompt.
From https://learn.microsoft.com/en-us/sql/machine-learning/troubleshooting/known-issues-for-sql-server-machine-learning-services?view=sql-server-ver15#7-unable-to-install-python-packages-using-pip-after-installing-sql-server-2019-on-windows
Worked for me.
pkg install openssl
Use this to enable ssl.
Currently there is same issue in Anaconda prompt (Anaconda3) on Windows 10. Here is workaround: https://github.com/ContinuumIO/anaconda-issues/issues/10576
Fixed this without having to change anything related to TSL/SSL.
I was trying to see if the same thing was happening to pip, and saw that pip was broken. Did some digging and realized it's probably caused by Homebrew deleted python#2 on February 1st, 2020.
Running brew uninstall python#2 to delete python2 installed by Homebrew.
Destroyed the virtual env created using python3 and created a new one. pip3 installing works fine again.
I am on macOS and I had used brew but what Vaulstein mentioned in his answer didn't cover my case.
I run the following commands to make sure my current python was not installed by brew
brew list | grep python
python
python#2
brew info python
python#3.8: stable 3.8.3 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Not installed
...
So I download the latest 3.8.5 from https://www.python.org/ and when installing it I saw following information
Certificate verification and OpenSSL
This package includes its own private copy of OpenSSL 1.1.1. The
trust certificates in system and user keychains managed by the
Keychain Access application and the security command line utility are not used as defaults by the Python ssl module
After installed 3.8.5 it fixed the problem.
I got into this problem using Ubuntu, pyenv and Python 3.8.1 managed by pyenv. There was actually no way to get pip to work correctly, since every time I tried to install anything, including pip itself, the same error showed up.
Final solution was to install, via pyenv, a newer version, in this case 3.8.6. Apparently, from 3.8.4 Python is prepared to run SSL/TLS out of the box, so everything worked fine.
I simply solved the problem with following command:
brew upgrade python#3.9
SSL is included by default on this version!
In my case I was running into issues with my $PATH on Linux. This can also happen on MacOS.
Check to see if /usr/bin/pip3 install package_name_goes_here works for you. If so then run
which pip3 this will tell you which is the first directory that pip3 is installed in.
If it is something like /usr/local/bin/pip3 which is different from /usr/bin/pip3 then you may need to adjust your $PATH.
Run
echo $PATH and copy the result.
The PATH is simply a colon separated list of directories that contain directories. Bash will always return the first instance of the program that you are attempting to execute. Move all the system directories upfront. Here is a list of some of the system directories:
/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
If that fails then verify you have openssl installed by running openssl version -a if not then install openssl.
If you've installed anaconda via scoop, and encounter this error while using pip from within a conda environment you can resolve it by...
Adding these to your path
C:\Users\YOUR_USERNAME\scoop\apps\anaconda3\current
C:\Users\YOUR_USERNAME\scoop\apps\anaconda3\current\Scripts
C:\Users\YOUR_USERNAME\scoop\apps\anaconda3\current\Library
C:\Users\YOUR_USERNAME\scoop\apps\anaconda3\current\Library\bin
Installing openssl via scoop
scoop install openssl
And copying the following DLLs from ..\anaconda3\Library\bin to ..\anaconda3\DLLs
References:
https://stackoverflow.com/a/54897379
https://stackoverflow.com/a/60405693

Support doesn't work

I'm trying to use neovim with deoplete and UtilSnips. Both requires Python support from nvim.
I followed the instructions in :help nvim_python to set the support but the output of :echo has('python') or :echo has('python3') are both 0.
On nvim-startup I get the message UltiSnips requires py >= 2.7 or py3 and for deoplete It requires Neovim with Python 3 support ("+python3").
My python (2.7.10) and python3 (3.4.3) are both installed with homebrew. The neovim module is installed over pip and pip3 with install neovim but nvim can't find it, even when I set the let g:python_host_prog path in nvimrc.
I don't know what I am able to do anymore, has anyone an idea whats wrong with it?
Please follow the instruction on https://neovim.io/doc/user/provider.html#provider-python to setup the python interpreter for neovim.
First, install pynvim (previously, it was named neovim, but that has been changed) plugin
pip3 install pynvim
Print g:loaded_python3_provider
echo g:loaded_python3_provider
" for python 2.x use the following
" echo g:loaded_python_provider
If it returns 1, the python is not setup for neovim. In your ~/.config/nvim/init.vim file, set the python interpreter
let g:python3_host_prog='/path/to/python3'
" for python2, use the following instead
"let g:python_host_prog = '/path/to/python2.7'
I encountered the same problem lately. Here are the steps adapted from answer of #VforVitamin where I made it working.
Assuming python2 and python3 are installed.
Install neovim plugin pip3 install neovim.
Open neovim.
Execute command :UpdateRemotePlugins.
Restart neovim.
I had the issue myself because I used neovim inside virtualenv. If so, make sure to pip install neovim inside your virtualenv, and make sure that import neovim works in the python interpreter.
If that doesn't help, you can try and run neovim with debug messages (neovim -V3, or any other logging level) and see what you can pick from there.
I bet you have a line in your init file that starts with "set runtimepath=".
Change it to "set runtimepath+="
If when you try
let [interp, errors] = provider#pythonx#Detect(2)
From the docs at
https://github.com/neovim/neovim/wiki/Troubleshooting#python-support-isnt-working
You get errors, could be that you have your VIM environment variable pointed to the wrong (probably vim74) runtime directory.
Neovim needs pythonx.vim from the runtime/autoload/providers/ folder to load a python interp. Vim74 doesn't provide this file.
If you have an env. variable of VIM (with a path), it will use that as your location of your runtime files - I had my set to /usr/share/vim/vim74, changing it to the location of neovim worked. I guess there is a compile time option to point to the correct location too.
I was with the same problem and the solution actually cames from the question.
What I did was:
pip install --upgrade pip
pip3 install --upgrade pip
sudo pip install setuptools
sudo pip3 install setuptools
sudo pip install neovim
sudo pip3 install neovim
After it, enter in neovim and :UpdateRemotePlugins. Close it and open again.
After these steps I had neovim working properly.
Edit:
For those using Arch Linux, we have a peculiarity about Python. The steps are:
Install pip (python3) and pip2 (python2): pacman -S python-pip python2-pip
Instead of pip3, you should use pip2
Beyond this minor difference, the rest of commands work pretty much the same way.
As pointed out by #fwalch, the documentation has changed to https://neovim.io/doc/user/provider.html#provider-python.
Neovim comes shipped with Python3 enabled, but you need to install the pynvim module in order to use Neovim Python3 plugins:
python3 -m pip install --user --upgrade pynvim

python - importing turtle module [duplicate]

Today I wanted to start working with Tkinter, but I have some problems.
Python 3.2 (r32:88445, Mar 28 2011, 04:14:07)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tkinter import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/tkinter/__init__.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
So how can I configure my Python 3.2 to work with Tkinter?
Under Arch/Manjaro just install the package tk:
sudo pacman -S tk
Solution for Linux, Windows (WSL/Ubuntu) and MacOS
After trying a bunch of things, this is how it finally worked:
$ brew install python-tk
Install tk-devel (or a similarly-named package) before building Python.
To get this to work with pyenv on Ubuntu 16.04 and 18.04, I had to:
$ sudo apt-get install python-tk python3-tk tk-dev
Then install the version of Python I wanted:
$ pyenv install 3.6.2
Then I could import tkinter just fine:
import tkinter
According to http://wiki.python.org/moin/TkInter :
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.
So appearantly many seems to have had this issue (me including) and I found the fault to be that Tkinter wasn't installed on my system when python was compiled.
This post describes how to solve the problem by:
Removing the virtual environment/python distribution
install Tkinter with sudo apt-get install tk-dev (for deb) or sudo pacman -S tk (for arch/manjaro)
Then proceed to compile python again.
This worked wonders for me.
I also faced similar problem. I resolved it by installing python-tk in my system.
Command for mac : brew install python-tk.
Had the same issue on Fedora with Python 2.7. Turns out some extra packages are required:
sudo dnf install tk-devel tkinter
After installing the packages, this hello-world example seems to be working fine on Python 2.7:
$ cat hello.py
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
$ python --version
Python 2.7.8
$ python hello.py
And through X11 forwarding, it looks like this:
Note that in Python 3, the module name is lowercase, and other packages are probably required...
from tkinter import *
Oh I just have followed the solution Ignacio Vazquez-Abrams has suggest which is install tk-dev before building the python.
(Building the Python-3.6.1 from source on Ubuntu 16.04.)
There was pre-compiled objects and binaries I have had build yesterday though, I didn't clean up the objects and just build again on the same build path. And it works beautifully.
sudo apt install tk-dev
(On the python build path)
(No need to conduct 'make clean')
./configure
make
sudo make install
That's it!
sudo apt-get install python3-tk
I encountered this issue on python 2.7.9.
To fix it, I installed tk and tcl, and then rebuild python code and reinstall, and during configure, I set the path for tk and tcl explicitly, by:
./configure --with-tcltk-includes="-I/usr/include" --with-tcltk-libs="-L/usr/lib64 -ltcl8.5 -L/usr/lib64 -ltk8.5"
Also, a whole article for python install process: Building Python from Source
Installing Tkinter
python -m pip install tk-tools
or
sudo apt install python3-tk
Code
from tkinter import *
root = Tk()
root.title('My App')
# Code
root.mainloop()
now i figured out what's going on ubuntu,
Follow these step to solve the issue
check your python version python3 --version
Lets Imagine you have python 3.10
Then Install Python-tk for the python version by using bellow command
sudo apt install python3.10-tk
simple if you have python3.8 then sudo apt install python{"use your python version here"}-tk
since I can not comment yet, here's my answer to another post:
since I'm still using python 3.9, this code works for me:
brew install python-tk#3.9
if using brew install python-tk brew will install python-tk#3.10 which is key-only
I think the most complete answer to this is the accepted answer found here:
How to get tkinter working with Ubuntu's default Python 2.7 install?
I figured it out after way too much time spent on this problem, so
hopefully I can save someone else the hassle.
I found this old bug report deemed invalid that mentioned the exact
problem I was having, I had Tkinter.py, but it couldn't find the
module _tkinter: http://bugs.python.org/issue8555
I installed the tk-dev package with apt-get, and rebuilt Python using
./configure, make, and make install in the Python2.7.3 directory. And
now my Python2.7 can import Tkinter, yay!
I'm a little miffed that the tk-dev package isn't mentioned at all in
the Python installation documentation.... below is another helpful
resource on missing modules in Python if, like me, someone should
discover they are missing more than _tkinter.
To anyone using Windows and Windows Subsystem for Linux, make sure that when you run the python command from the command line, it's not accidentally running the python installation from WSL! This gave me quite a headache just now. A quick check you can do for this is just
which <python command you're using>
If that prints something like /usr/bin/python2 even though you're in powershell, that's probably what's going on.
If you're running on an AWS instance that is running Amazon Linux OS, the magic command to fix this for me was
sudo yum install tkinter
If you want to determine your Linux build, try cat /etc/*release
This symptom can also occur when a later version of python (2.7.13, for example) has been installed in /usr/local/bin "alongside of" the release python version, and then a subsequent operating system upgrade (say, Ubuntu 12.04 --> Ubuntu 14.04) fails to remove the updated python there.
To fix that imcompatibility, one must
a) remove the updated version of python in /usr/local/bin;
b) uninstall python-idle2.7; and
c) reinstall python-idle2.7.
Even after installing python-tk, python3-tk I was getting error your python is not configured for Tk.
So I additionally installed tk8.6-dev
Then I build my Python again, run following again:
make,
make install.
When I did this I saw messages on screen that it is building _tkinter and related modules. Once that is done, I tried 'import tkinter" and it worked.
If you are using Manjaro(Arch Linux) run below command in your terminal
sudo pacman -S tk

Categories