RHEL/Pyenv: No module named '_tkinter' - python

I'm trying to use Matplotlib on RHEL using a pyenv installed version of python 3.6.5. (installed using the following command)
→ pyenv install 3.6.5
Installing Python-3.6.5...
python-build: use readline from homebrew
Installed Python-3.6.5 to /home/swp1g17/.pyenv/versions/3.6.5
→ pyenv global 3.6.5
I'm presented with the following error, and have found many questions that have a similar issue:
Python 3.6.5 (default, Apr 5 2018, 17:22:36)
[GCC 5.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/swp1g17/.pyenv/versions/3.6.5/lib/python3.6/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
However the suggested solution is usually to install tkinter using a package manager.
However I've tried installing:
2872 sudo yum install rh-python36-python-tkinter
2873 sudo yum install rh-python35-python-tkinter
2874 sudo yum install rh-python34-python-tkinter
2891 sudo yum install tkinter
2893 sudo yum install python36-tkinter
2902 sudo yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel gdbm-devel ncurses-devel gl..
2916 sudo yum install tkinter.x86_64 rh-python36-python-tkinter.x86_64 rh-python35-python-tkinter.x86_64 rh-python34-python-tkinter.x86_64 p..
2921 sudo yum install tcl
2933 sudo yum install tk-devel
2934 sudo yum install tk
3000 sudo yum install tkinter
3026 sudo yum install tix
3031 sudo yum install tk
3032 sudo yum install tk-devel
> 3033 sudo yum install tcl-devel
with each already having been installed or making no difference (having rebuilt python each time a new package was installed.
The system python is able to locate tkinter:
→ /usr/bin/python3.6
Python 3.6.3 (default, Jan 4 2018, 16:40:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
so I'm unsure how to install python using pyenv and have it use the same version of tkinter?
UPDATE:
Having found that build configuration options can be set using $PYTHON_CONFIGURE_OPTS I've tried specifying the library locations using (for linuxbrew downloaded tcl/tk):
export PYTHON_CONFIGURE_OPTS="--with-tcltk-includes=-I/home/linuxbrew/.linuxbrew/opt/tcl-tk/include --with-tcltk-libs=-L/home/linuxbrew/.linuxbrew/opt/tcl-tk/lib"
pyenv install 3.6.5
and (using system tcl/tk):
export PYTHON_CONFIGURE_OPTS="--with-tcltk-includes=-I/usr/include --with-tcltk-libs=-L/usr/lib64"
pyenv install 3.6.5
each with no luck. System tcl/tk was found using:
→ whereis tcl
tcl: /usr/lib64/tcl8.5 /usr/include/tcl.h /usr/share/tcl8.5
→ whereis tcl
tcl: /usr/lib64/tcl8.5 /usr/include/tcl.h /usr/share/tcl8.5

Below is my step-by-step howto using pyenv 1.2.11 on Ubuntu 19.04 and Fedora 29:
Install pyenv
Using the installer
git clone https://github.com/pyenv/pyenv-installer
chmod +x pyenv-installer/bin/pyenv-installer
pyenv-installer/bin/pyenv-installer
By hand
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Setup pyenv
$ cat >> ~/.bashrc
# Load pyenv automatically
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
Ctrl + D
$ source ~/.bashrc
$ pyenv --version
pyenv 1.2.11-8-g070e1c85
Install dependencies
Ubuntu 19.04 Disco Dingo
sudo apt install tk-dev # <-- This has fixed my issue
sudo apt install libbz2-dev
sudo apt install libreadline-dev
sudo apt install libsqlite3-dev
Fedora 29
sudo dnf install tk-devel # <-- This may fix your issue
sudo dnf install libbz2-dev
sudo dnf install readline-devel
sudo dnf install libsqlite3x-devel
sudo dnf install openssl-devel
sudo dnf install make
Install Python-3.6.8 using pyenv
pyenv install 3.6.8
Check installation
$ pyenv local 3.6.8 # Create file .python-version
$ python --version
Python 3.6.8
Install modules
python -m pip install --user --upgrade pip
python -m pip install --user pipenv
...
Example of pipenv usage
cd /path/where/your/Pipfile/is/located/
pyenv local 3.6.8
python -m pipenv --rm # first time: No virtualenv has been created...
python -m pipenv update --dev
python -m pipenv run python -m nose # my unit tests

Related

How can I install a legacy PIP version with python 2.6.6 or python 2.7.5?

I'm struggling to install the python pip module on my centOS server which is running on python2.6.6(centOS 6) or python 2.7.5(CentOS7). Due to some reasons I'm not able to upgrade the python version to 3 or later. So in the case, how can I install a legacy PIP version line pip 20 with my python2.6 or python 2.7 platform?
Thanks.
You can use get-pip.py for 2.7. Here's an example in a centos:7 Docker container:
$ python -V
Python 2.7.5
$ curl -fsSL -O https://bootstrap.pypa.io/pip/2.7/get-pip.py
$ python get-pip.py --no-python-version-warning && rm -f get-pip.py
$ python -m pip --version
pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7)
Docs: https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py. (Though admittedly, they don't go out of their way to point out the presence of the 2.6/2.7 branches, given that Python 2 is prominently EOL.)
If you have Python 2, you most likely have pip. Try python2 -m pip install foobar.
Turns out that in CentOS7 pip is not available in distro repos by default. You need to yum install epel-release to enable the EPEL repo, which in turn contains pip: yum install python-pip.
So do this: (as root/using sudo)
yum install epel-release
yum makecache
yum install python-pip

How to Install pip for python 3.7 on Ubuntu 18?

I've installed Python 3.7 on my Ubuntu 18.04 machine. Following this instructions in case it's relevant:
Download : Python 3.7 from Python Website [1] ,on Desktop and manually
unzip it, on Desktop Installation : Open Terminal (ctrl +shift+T)
Go to the Extracted folder
$ cd ~/Desktop/Python-3.7.0
$ ./configure
$ make
$ sudo make install
Making Python 3.7 default Python :
$ sudo vim ~/.bashrc
press i
on the last and new line - Type
alias python= python3.7
press Esc
type - to save and exit vim
:wq
now type
$ source ~/.bashrc
From here: https://www.quora.com/How-can-I-upgrade-Python-3-6-to-3-7-in-Ubuntu-18-04
I've downloaded several modules through pip install module but when I try to import them, I get a ModuleNotFoundError: No module names 'xx'
So I did some research and apparently when used pip to install, it installed in the modules in previous version of Python.
Somewhere (probably a question in SO) I found a suggestion to install the module using python3.7 -m pip install module but then I get /usr/local/bin/python3.7: no module named pip.
Now I'm stuck, pip is installed, but apparently not for Python 3.7. I'm assuming that if I can install pip for Python 3.7, I can run the pip install command and get the modules I need.
If that is the case, how can I install pip for python 3.7, since it's already installed?
This is the best I have come up with:
I have installed python 3.7 successfully and I can install modules using pip (or pip3) but those modules are installed in Python 3.6 (Comes with ubuntu). Therefore I can't import those modules in python 3.7 (get a module not found)
Python 3.7 doesn't recognize pip/pip3, so I can't install through pip/pip3
I need python 3.7
In general, don't do this:
pip install package
because, as you have correctly noticed, it's not clear what Python version you're installing package for.
Instead, if you want to install package for Python 3.7, do this:
python3.7 -m pip install package
Replace package with the name of whatever you're trying to install.
Took me a surprisingly long time to figure it out, too. The docs about it are here.
Your other option is to set up a virtual environment. Once your virtual environment is active, executable names like python and pip will point to the correct ones.
A quick add-on to mpenkov's answer above (didn't want this to get lost in the comments)
For me, I had to install pip for 3.6 first
sudo apt install python3-pip
now you can install python 3.7
sudo apt install python3.7
and then I could install pip for 3.7
python3.7 -m pip install pip
and as a bonus, to install other modules just preface with
python3.7 -m pip install <module>
EDIT 1 (12/2019):
I know this is obvious for most. but if you want python 3.8, just substitute python3.8 in place of python3.7
EDIT 2 (5/2020):
For those that are able to upgrade, Python 3.8 is available out-of-the-box for Ubuntu 20.04 which was released a few weeks ago.
This works for me.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Then this command with sudo:
python3.7 get-pip.py
Based on this instruction.
I used apt-get to install python3.7 in ubuntu18.04. The installations are as follows.
install python3.7
sudo apt-get install python3.7
install pip3. It should be noted that this may install pip3 for python3.6.
sudo apt-get install python3-pip
change the default of python3 for python3.7. This is where the magic is, which will make the pip3 refer to python3.7.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
Hope it works for you.
To install all currently supported python versions (python 3.6 is already pre-installed) including pip for Ubuntu 18.04 do the following:
To install python3.5 and python3.7, use the deadsnakes ppa:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5
sudo apt-get install python3.7
Install python2.7 via distribution packages:
sudo apt install python-minimal # on Ubuntu 18.04 python-minimal maps to python2.7
To install pip use:
sudo apt install python-pip # on Ubuntu 18.04 this refers to pip for python2.7
sudo apt install python3-pip # on Ubuntu 18.04 this refers to pip for python3.6
python3.5 -m pip install pip # this will install pip only for the current user
python3.7 -m pip install pip
I used it for setting up a CI-chain for a python project with tox and Jenkins.
Combining the answers from #mpenkon and #dangel, this is what worked for me:
sudo apt install python3-pip
python3.7 -m pip install pip
Step #1 is required (assuming you don't already have pip for python3) for step #2 to work. It uses pip for Python3.6 to install pip for Python 3.7 apparently.
When i use apt install python3-pip, i get a lot of packages need install, but i donot need them. So, i DO like this:
apt update
apt-get install python3-setuptools
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
rm -f get-pip.py
The following steps can be used:
sudo apt-get -y update
---------
sudo apt-get install python3.7
--------------
python3.7
-------------
curl -O https://bootstrap.pypa.io/get-pip.py
-----------------
sudo apt install python3-pip
-----------------
sudo apt install python3.7-venv
-----------------
python3.7 -m venv /home/ubuntu/app
-------------
cd app
----------------
source bin/activate
Install python pre-requisites
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Install python 3.7 (from ppa repository)
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7
Install pip3.7
sudo apt install python3-pip
python3.7 -m pip install pip
Create python and pip alternatives
sudo update-alternatives --install /usr/local/bin/python python /usr/bin/python3.7 10
sudo update-alternatives --install /usr/local/bin/pip pip /home/your_username/.local/bin/pip3.7 10
Make changes
source ~/.bashrc
python --version
pip --version
For those who intend to use venv:
If you don't already have pip for Python 3:
sudo apt install python3-pip
Install venv package:
sudo apt install python3.7-venv
Create virtual environment (which will be bootstrapped with pip by default):
python3.7 -m venv /path/to/new/virtual/environment
To activate the virtual environment, source the appropriate script for the current shell, from the bin directory of the virtual environment. The appropriate scripts for the different shells are:
bash/zsh – activate
fish – activate.fish
csh/tcsh – activate.csh
For example, if using bash:
source /path/to/new/virtual/environment/bin/activate
Optionally, to update pip for the virtual environment (while it is activated):
pip install --upgrade pip
When you want to deactivate the virtual environment:
deactivate
I installed pip3 using
python3.7 -m pip install pip
But upon using pip3 to install other dependencies, it was using python3.6.
You can check the by typing pip3 --version
Hence, I used pip3 like this (stated in one of the above answers):
python3.7 -m pip install <module>
or use it like this:
python3.7 -m pip install -r requirements.txt
I made a bash alias for later use in ~/.bashrc file as alias pip3='python3.7 -m pip'. If you use alias, don't forget to source ~/.bashrc after making the changes and saving it.
How about simply
add-apt-repository ppa:deadsnakes/ppa
apt-get update
apt-get install python3.7-dev
alias pip3.7="python3.7 -m pip"
Now you have the command
pip3.7
separately from pip3.
curl https://bootstrap.pypa.io/get-pip.py | sudo python3.7
if all else fails.
pip3 not pip. You can create an alias like you did with python3 if you like.

Recommended way to install pip(3) on centos7

I am interrested in knowing the recommended way to install pip3 for python3.6 (as of today, may 2018) on current version of centos7 (7.5.1804) and the accepted answer of How to install pip in CentOS 7? seems to be outdated because:
yum search -v pip
outputs (among other things):
python2-pip.noarch : A tool for installing and managing Python 2 packages
Repo : epel
python34-pip.noarch : A tool for installing and managing Python3 packages
Repo : epel
and python34-pip seems to be a (newer?) simpler way than the accepted answer of How to install pip in CentOS 7? :
sudo yum install python34-setuptools
sudo easy_install-3.4 pip
But since the versions of python installed on my machine are 2.7.5 and 3.6.3 why is it python34-pip and not python36-pip ? Is pip the same for 3.4+ (up to current 3.6.3) ?
Is pip the same for 3.4+
No, it's not. A single pip installation serves a single Python distribution (pip2.7/pip3.4/pip3.5 etc).
Since Python 3.5, pip is already bundled with the python distribution, so you can just run python3.6 -m pip instead of pip.
Python 3.6 is not available in CentOS 7 vanilla repo. I usually resort to IUS repo when needing to install a fresh Python on CentOS. It always has the most recent Python version, the current one being 3.6.5. It also offers a correspondent pip package.
$ yum install https://centos7.iuscommunity.org/ius-release.rpm
$ yum install python36u python36u-devel python36u-pip
Unfortunately, IUS doesn't offer a package for Python 3.7 yet so if you are looking for Python 3.7 on CentOS 7, building from source is your only option.
Edit: when yum is not an option
You should prefer the bootstrapping solution described in this answer as it is the most reliable way to get a working pip installed.
To install pip for python 3.6 on CentOS 7 you need to run
$ python3.6 -m ensurepip
Follow these commands in Centos 7
yum install python36
yum install python36-devel
yum install python36-setuptools
easy_install-3.6 pip
to check the pip version:
pip3 -V
pip 18.0 from /usr/local/lib/python3.6/site-packages/pip-18.0-py3.6.egg/pip (python 3.6)
There is now a python3-pip package in the CentOS 7 base repository as of 2019-08-22. There is no longer a need for third-party repositories or packages.
Installing python3-pip will also install libtirpc, python3, python3-libs, and python3-setuptools:
yum install --assumeyes python3-pip
You can now verify the version (yes, it is old, but it's what is coming from the base repository):
$ pip3 --version
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
If you don't want to stray from the files provided by the python3-pip package, and you don't want to see warnings about pip being old, see https://stackoverflow.com/a/46288945/534275 for silencing the messages.
Pip is not bundled in the EPEL version of python 3.6 for some reason. I assume a decent amount of people (such as me) will find this page because of that.
$ sudo yum install -y python36
...
$ python36 -m pip
/usr/bin/python36: No module named pip
So in this case, the setuptools package was the easiest solution.
$ sudo yum install python36-setuptools
$ sudo easy_install-3.6 pip
...
$ python36 -m pip --version
pip 18.0 from /usr/local/lib/python3.6/site-packages/pip-18.0-py3.6.egg/pip (python 3.6)
In case you're seeing that pip3 is linked to python2 path:
$ pip3 -V
pip 8.1.2 from /usr/lib/python2.7/site-packages/pip (python 2.7)
You'll probably get:
$ pip3 install --upgrade pip
TypeError: parse() got an unexpected keyword argument 'transport_encoding'
Then try to clear commands cache with hash -r. This has worked for me:
# Install Python 3:
sudo yum install python36 -y
# Install & Upgrade pip3
sudo python36 -m pip install --upgrade pip
# Validate pip3 installation:
sudo python3.6 -m ensurepip
# Successfully installed pip-10.0.1 setuptools-39.0.1
# Clear commands cache
hash -r
# might be required if getting in bash: /usr/bin/pip3: No such file or directory)
pip3 -V
# pip 19.0.3 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
which pip3
# /usr/local/bin/pip3
pip2 -V
# pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)
which pip2
# /usr/local/bin/pip2
# Install your Python3 module:
sudo /usr/local/bin/pip3 install {required module for python3}
I got this error when I tried to install python36 over the built-in centos7 python 2.7.5 version:
Transaction check error:
file /etc/rpm/macros.python from install of python-rpm-macros-3-32.el7.noarch conflicts with file from package python-devel-2.7.5-80.el7_6.x86_64
And I fixed it with these:
yum install python36
yum update python-devel
yum install python36-devel
easy_install-3.6 pip
Try This::
sudo yum update
sudo yum install -y python36u python36u-libs python36u-devel python36u-pip
Working for me perfectly.

how to install python3-tk in centos?

I need to install python3-tk in order to use matplotlib.
I have tried:
(python_3.4_numerical) [lpuggini#machinelearn-1 ~]$ sudo yum install python3-tk
[sudo] password for lpuggini:
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.clouvider.net
* epel: epel.check-update.co.uk
* extras: mirror.sov.uk.goscomb.net
* updates: mirrors.clouvider.net
No package python3-tk available.
Error: Nothing to do
(python_3.4_numerical) [lpuggini#machinelearn-1 ~]$
but it is not working.
How can I fix it?
EDIT:
Installing from pip does not work:
(python_3.4_numerical) [lpuggini#machinelearn-1 ~]$ pip install pygtk
Collecting pygtk
Using cached pygtk-2.24.0.tar.bz2
Complete output from command python setup.py egg_info:
********************************************************************
* Building PyGTK using distutils is only supported on windows. *
* To build PyGTK in a supported way, read the INSTALL file. *
********************************************************************
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-nzjsuhx3/pygtk/
(python_3.4_numerical) [lpuggini#machinelearn-1 ~]$
tkinter is available in coreos as tkinter package. You can install it with
sudo yum install tkinter
Once it is done, you can import and use it as usual.
>>> import tkinter
>>> tkinter._test()
For Python 3, you can install it with
sudo yum install python3-tkinter
As some users mentioned, it is available as python36u-tkinter or python34-tkinter depending on OS.
sudo yum install python34-tkinter
sudo yum install python36u-tkinter
In Centos 7 you can use:
yum install python36-tkinter
I solved the same problem using these two commands 100%
sudo yum -y update
sudo yum -y install python36u-tkinter
Try the following command.
sudo yum install python3-tkinter
Now Test in your terminal:
>>> import tkinter
>>> tkinter._test()
I believe tk come with python by default. If so, have you look to reinstall your python 3.4. (I prefer ver3.5). Procedures are mentioned in this website.
Steps:
Download python version
wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
Install decoding tool if you don't have: sudo yum install xz-libs
Decode the xz encodeing xz -d Python-3.5.3.tar.xz
Uncompress the decoded file tar -xvf Python-3.5.3.tar
Move the desired directory to install python cd Python-3.5.3
Configure python file ./configure
Build using make
Install using make altinstall
Check if tkinter works. A tk window should pop out. Open a terminal and type the following:
python3.5
import tkinter as tk
root = tk.Tk()
The website also has other instructions for installing setuptools and pip which are very useful.
I spent lots of time on this problem, here is my solution effectively:
yum -y install tkinter tcl-devel tk-devel
to your python directory:
vim ......./Python3.5.1/Modules/Setup.dist
Remove comments from these lines:
_tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \
-L/usr/local/lib \
-I/usr/local/include \
-ltk8.5 -ltcl8.5 \ # default is 8.2 and you should change it to the version you installed in step1
-lX11
3.
./configure
make && make install
Result:
[root#localhost Python-3.5.1]# python3
Python 3.5.1 (default, Jun 19 2017, 22:43:42)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>>
It depends on your python version. For python 3.4 it's sudo yum install python34-tkinter
Or, maybe you are using software collections (SCL), then install the proper tkinter.
search tkinter:
yum search tkinter
install proper tkinter such as
yum install rh-python36-python-tkinter
Depending on which repository you are using for python, you have different "default" tkinter.
I tried all the other solutions here and none of them work for me.
After a bit of research, I found the right package that worked for me.
sudo yum update
sudo yum install rh-python36-python-tkinter
if you use a different version of python3 try searching for your package as I did with
sudo yum search tkinter
and see what option fits your version
for python 3.9 version and on RHEL:
$ sudo yum install python39-tkinter

Pygame on Ubuntu Python 3

I have searched everywhere. There is no answer for my specific problem! I have Ubuntu, I want to use pygame on Python 3. I installed the pygame package with
sudo apt-get python-pygame
but it just doesn't work when I type
import pygame
Please help!
You must install pip for python3
$ sudo apt install python3-pip
and then, with the pip installed, install pygame
$ pip3 install pygame
Ubuntu 19.04 and later
To install python3-pygame in Ubuntu 19.04 and later open the terminal and type:
sudo apt install python3-pygame
Ubuntu 18.10
To install python3-pygame in Ubuntu 18.10 open the terminal and type:
sudo nano /etc/apt/sources.list
Add this line to sources.list.
deb http://archive.ubuntu.com/ubuntu/ cosmic-proposed universe
Save sources.list with the keyboard combination Ctrl+O and press Enter and exit with Ctrl+X
Update the list of available software and install python3-pygame.
sudo apt update
sudo apt install python3-pygame
Ubuntu 16.04-18.04
sudo apt install python3-setuptools
sudo easy_install3 pip
python3 -m pip install --user pygame
The pygame package on Ubuntu is only for python 2.7, so it is impossible to import if you use python 3. If you want python 3.x support, you need to install pygame 1.9.2 which is located at https://bitbucket.org/pygame/pygame/downloads
If you have all the dependencies installed, it should be as simple as running setup.py
I'm at a Pop-OS 20.04 machine and all of those answers didn't work for me (◉_◉). So I went to the documentation of Pygame and realized that I had to have the dependencies installed. To do that you can:
sudo apt-get install git python3-dev python3-setuptools python3-numpy python3-opengl \
libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \
libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev \
libtiff5-dev libx11-6 libx11-dev fluid-soundfont-gm timgm6mb-soundfont \
xfonts-base xfonts-100dpi xfonts-75dpi xfonts-cyrillic fontconfig fonts-freefont-ttf libfreetype6-dev
Now you are ready to install Pygame, so git clone their GitHub repository into your current folder and pip install the module:
git clone https://github.com/pygame/pygame.git
If you are using Python 2 go to this URL: https://www.pygame.org/wiki/CompileUbuntu?parent=

Categories

Resources