This is my first post StackOverflow, I will try to make it as correct and complete as possible if you have any tips to improve my post I will gladly accept it.
I'm having trouble running code written in Python that uses Tkinter.
I will try to describe in detail my actions to facilitate the identification of the error.
I started a course at Coursera on DSP (Digital Signal Processing) where it is suggested to install a tool written in python (and a little bit of C). I'm using Arch Linux.
link on Github:
sms-tools repo
Using pyenv/virtualenv/virtualenvwrapper I created an environment with Python 3.7.5, as recommended in the "How to use" section of the repository.
I installed the required libraries in my environment by pip:
%pip install ipython numpy matplotlib scipy cython
I compiled some C functions in the "/sms-tools/software/models/utilFunctions_C"
directory with the following command:
%python compileModule.py build_ext --inplace
Finally, I run the models GUI in the directory "/sms-tools/software/models_interface"
%python models_GUI.py
and I get the following message:
Traceback (most recent call last):
File "models_GUI.py", line 6, in <module>
from Tkinter import * ## notice capitalized T in Tkinter
ModuleNotFoundError: No module named 'Tkinter'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "models_GUI.py", line 9, in <module>
from tkinter import * ## notice lowercase 't' in tkinter here
File "~/.pyenv/versions/3.7.5/lib/python3.7/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'
I will now describe some of my attempts to solve the problem:
Looking at Tkinter section in Python Wiki I tried installing Tcl and Tk.
%sudo pacman -S tk
but it was already installed. after that I tried installing with pip:
%pip install tk
and
%pip install tkinter
and the error remains the same.
I also tried to create a symlink with this code:
%ln -s /usr/lib/python3.8/lib-dynload/_tkinter.cpython-38-x86_64-linux-gnu.so _tkinter.cpython-38-x86_64-linux-gnu.so
the symlink was created in the following folders:
~/.ve/Coursera_DSP/lib/python3.7/lib-dynload
and
.pyenv/versions/3.7.5/lib/python3.7/lib-dynload
But I still get the same error.
I appreciate it if anyone has any suggestions and I apologize for the language errors since English is not my mother tongue.
After an incessant search on the internet, I believe the problem is related to pyenv and TCL/TK.
I don't understand much about the subject but I suspect that in the creation of the environment by virtualenv python has lost the connection with TCL/TK. Does that make any sense?
Here is step by step guide to make IDLE and tkinter work. Working for me on macOS Catalina. Should be easily adapted to Linux environment:
install tcl-tk with Homebrew. In shell run brew install tcl-tk
in shell run echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
reload shell by quitting Terminal app or run source ~/.zshrc
after reloaded check that tck-tk is in $PATH. Run echo $PATH | grep --color=auto tcl-tk. As the result you should see your $PATH contents with tcl-tk highlighted
now we run three commands from Homebrew's output from step #1
in shell run export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
in shell run export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
in shell run export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
if you have your Python version already installed with pyenv then uninstall it with pyenv uninstall <your python version>. E.g. pyenv uninstall 3.8.2
set environment variable that will be used by python-build. In shell run PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
Note: in future use tck-tk version that actually installed with Homebrew. At the moment of posting 8.6 was the actual
finally install Python with pyenv with pyenv install <version>. E.g. pyenv install 3.8.2
Test
in shell run pyenv global <verion that you've just installed>
now check IDLE. In shell run idle. You should see IDLE window without any warnings and "text printed in red".
now check tkinter. In shell run python -m tkinter -c "tkinter._test()". You should see test window like on the image:
That's it!
My environment:
check this is something went wrong executing steps above:
macOS Catalina
zsh (included in macOS Catalina) = "shell" above
Homebrew (installed with instructions from Homebrew official website)
pyenv (installed with Homebrew and PATH updated according to pyenv official readme from GitHub)
Python 3.8.x - 3.9.x (installed with pyenv install <version> command)
mac Monterey (M1)
brew install python-tk
python 3.9.10
My basic install of linux mint 20 contains python3.
On windows machines this includes the tkinter package.
My solution was to
sudo apt-get install python-tk
or
sudo apt-get install python3-tk
and live was sunny again.
I created a venv using Python 3.10.5 (alternate version to default 3.8). Tkinter failed to install when I used the command "sudo apt-get install python3-tk"; however, when I used "sudo apt-get install python3.10-tk", the module installed properly. Apparently, using the specific python3 version made the difference.
Since I couldn't find any solution I just uninstalled my whole environment system (pyenv pyenv-virtualenv virtualenv and virtualenvwrapper) and installed conda instead. Now everything works.
Probably I messed up with pyenv installation.
Thank you all :D
Related
I would like to preface with the fact that I am a coding newbie and all this is very new to me. In fact, this is my first stack overflow question.
Anyways I am learning python and am trying to install my first third party module called “pyperclip” in Terminal, but I don't think it installed correctly. I am very new with Terminal as well.
For reference, I am following the youtube guide "Automate the Boring Stuff with Python"
https://www.youtube.com/watch?v=xJLj6fWfw6k
I am on Lesson 8 and I am at minute 3:43 in the video if you want to follow along.
Here are some of my system details:
Mac OS X
Python version 3.8.3
I ran the following into Terminal:
sudo pip3 install pyperclip
Then I received the following message in Terminal after installing pyperclip
The directory or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
I then tried to install again using the following:
sudo -H pip3 install pyperclip
Then I got this message:
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
I tried installing one last time using:
pip3 install pyperclip
I got the same message again:
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
This is also the error I get when I try importing pyperclip in my interactive shell:
>>> import pyperclip
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ModuleNotFoundError: No module named ‘pyperclip’
>>>
Can anyone help me please? I feel like a fish out of the water with all this stuff. Especially when it comes to Terminal. Any help would be greatly appreciated.
I wanted to discuss a few things that might help you on your way.
We typically do not use sudo in conjunction with pip to install Python packages. This installs the package at a system level and could conflict with packages that your system currently has installed or may install packages to a Python installation which is different than the one you are using from the terminal. Instead, we typically use pip install --user rather than sudo pip install.
See this question and answer for more: sudo pip install VS pip install --user.
If you are ever unsure whether a package has been installed properly, check using
pip3 list
We also need to make sure that the Python interpreter you are using is the same as where pip is installing. Since you are using pip3 to install, you should be using python3 at the terminal to enter the interactive shell. You can also verify that you are using the right Python interpreter by typing in the following command into the terminal:
which python3
Then, make sure that the output matches with the /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ path that was reported by pip3.
If you are using a different interactive shell such as ipython, you need to make sure that you install ipython in the same manner using
pip3 install --user ipython
and executing it using
ipython3
Try repeating your steps using this new information to see if it helps. Let me know if you have any more questions or need some more help.
I have installed Python 3.6 using the instructions at https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
For reasons I don't fully understand Python 3.6.5 was placed in
/home/pi/Python-3.6.5
and some modules were not available.
I installed RPi.GPIO into /home/pi/Python-3.6.5/Lib using
pip install RPi.GPIO -t .
Attempting to run a script with sudo python3.6 /home/pi/PythonProjects/omxcall_radar.py produces this error:
Traceback (most recent call last):
File "/home/pi/PythonProjects/omxcall_radar.py", line 18, in <module>
import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'
However the RPi directory is in the Lib directory
I am working at the limit of my linux skills. Can someone please explain why I am getting the error and/or what I may have done wrong in installing Python 3.6/RPi.GIPO?
There's a lot going on here.
The Python-3.6.5 directory in /home/pi is the Python source code. It's there because that's where you unpacked the Python-3.6.5.tar.xz archive you retrieved using wget. It is not where Python was installed.
When you ran sudo make altinstall, that installed Python into /usr/local (specifically, the binary itself would be /usr/local/bin/python3.6).
When you ran pip install RPi.GPIO -t ., you installed the RPi.GPIO module into your current directory, but that's not anywhere that your newly installed Python would look for it. Additionally, the pip you were using doesn't know anything about your new Python install, either.
After running make altinstall, you should probably do the following:
Install pip for your new Python install:
easy_install-3.6 pip
Use pip3.6 to install RPi.GPIO:
pip3.6 install RPi.GPIO
Finally, run your script using your new Python:
python3.6 /home/pi/PythonProjects/omxcall_radar.py
You may or may not need sudo, depending on what sort of access your script requires.
I am trying to run python sphinx on a CentOS-7 VM, using a virtual python 3.6.1 environment.
Steps:
Create python3 virtual environment
$ pyvenv myapp
$ source myapp/bin/activate
Install required packages for myproj
$ pip install -r requirements.txt
Install sphinx
$ pip install sphinx sphinx-autobuild
Try and run the quickstart:
$ sphinx-quickstart
File "/bin/sphinx-quickstart", line 7 in <module>
from sphinx.quickstart import main
ImportError: No module named sphinx.quickstart
pip freeze indicates that I am running the following versions:
Sphinx==1.6.3
sphinx-autobuild==0.7.1
pyvenv has been deprecated. See note in the Python library docs:
Note: The pyvenv script has been deprecated as of Python 3.6 in favor of using python3 -m venv to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on.
Instead first make sure you have not already activated a virtual environment. Then issue the command:
python3 -m venv myapp
The rest of your steps look OK.
I solved the problem. I am new to python and pyvenv, and apparently it was finding a "universally" installed version of sphinx outside of the virtual environment, and that was preempting the version that was installed inside the virtualenv. I solved the problem by specifying the full path:
/full-path-to-myapp/myapp/bin/sphinx-quickstart
I'm having a little trouble with virtualenv on Mac OS X Yosemite. After I couldn't run virtualenv at all first, I installed Python 3 via brew (previously I installed it via the package on python.org). I linked this installation of python3, updated pip and ran pip3 install virtualenv. When I try to run virtualenv (e.g. $ virtualenv --python=python3 ../virtualenv), I get the following error message.
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 7, in <module>
from virtualenv import main
File "/usr/local/bin/virtualenv.py", line 7, in <module>
from virtualenv import main
ImportError: cannot import name 'main'
Can anybody help me with this?
After my upgrade to Fedora 32 I had the same issue which lead me to this question:
ImportError: cannot import name 'main' from 'virtualenv'
In my case I actually seemed to have both /usr/local/bin/virtualenv as well as $HOME/.local/lib/python3.8/site-packages/virtualenv/__init__.py.
Removing the user virtualenv version and reinstalling it into the system with root fixed the issue:
pip uninstall virtualenv
sudo pip install virtualenv
Your virtualenv executable /usr/local/bin/virtualenv is importing the virtualenv package /usr/local/bin/virtualenv.py. My guess is that package is not the one the executable should really be importing. The reason it is choosing that one is because it is in the same directory.
First, check where the real virtualenv package is. In the python3 terminal:
>>> import virtualenv
>>> virtualenv.__file__
If it is not /usr/local/bin/virtualenv.py, then the simplest way to get /usr/local/bin/virtualenv to import it instead of /usr/local/bin/virtualenv.py is to delete /usr/local/bin/virtualenv.py (or so you can easily undo this if it doesn't work, simply rename virtualenv.py to something else like xvirtualenvx.py).
I received this error after upgrading Ubuntu 18.04 LTS to 20.04 LTS. So there were two problems all at once. First the python version was still running 2.x and doing a simple update or try to uninstall (apt-get remove virtualenv) of virtualenv did not help at all. But I found a solution. First let 20.04 LTS 'know' the times of using old python is over:
sudo apt-get install python-is-python3
Then test it and open a console to get the version string with python -V; by now it should be showing something like Python 3.8.5. Fine.
Next step is to solve the virtualenv problem. I tried to find out, which executable was run with which virtualenv and it showed: $HOME/.local/bin/virtualenv. Hmmkay, somehow the system wasn't using the /usr/bin/virtualenv executable. I thought maybe I let the directory become invisible (a.k.a. renaming) and maybe the system will go on a hunt for an alternative virtualenv running:
mv $HOME/.local/bin/virtualenv /home/USER/.local/bin/virtualenv_OLD
Then I simply changed into a playground-directory and ran virtualenv donaldknuth and behold - it worked. To be sure I ran another which virtualenv and the system returned a /usr/bin/virtualenv. Last check to do was activating the new virtual environment:
source $HOME/playground/donaldknuth/bin/activate
The terminal changed and it worked fine. Solution
EDIT:
Based on Pierre B.'s suggestion you may have to restart your Shell. The command hash -d virtualenv will delete the stored location of virtualenv from the shell's cache and determine the correct path right now. (Sources: https://www.computerhope.com/unix/bash/hash.htm, https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables)
Similarly to some others here, I had multiple installations of virtualenv. Not sure where the extra one came from, but I had these two:
/usr/local/bin/virtualenv
/usr/bin/virtualenv
One is from apt install of virtualenv, the other from pip install of virtualenv.
This happened when upgrading to Ubuntu 20.04.
On Linux Mint 20, I had to switch default Python interpreter to python3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
Then remove existing virtualenv and reinstall via pip and python3:
rm ~/.local/bin/virtualenv
apt remove python3-virtualenv
sudo pip install virtualenv
I have python3.3.1 on ubuntu lucid,which I invoke thru virtualenvwrapper ' .I wanted to learn pygame,so I used pip to install it.Before that I installed the sdl and smpeg dev libraries
me#ubuntu: sudo apt-get install libsdl1.2-dev
...
me#ubuntu: sudo apt-get install libsmpeg-dev
...
me#ubuntu: workon envpy331
(envpy331)me#ubuntu:~$ pip install pygame
Downloading pygame-1.9.1release.tar.gz (2.1MB): 2.1MB downloaded
Running setup.py egg_info for package pygame
WARNING, No "Setup" File Exists, Running "config.py"
Using UNIX configuration...
Hunting dependencies...
SDL : found 1.2.14
FONT : not found
IMAGE : not found
MIXER : not found
SMPEG : found 0.4.5
PNG : found
JPEG : found
SCRAP : found
PORTMIDI: not found
PORTTIME: not found
....
Continuing With "setup.py"
Successfully installed pygame
Cleaning up...
Then I tried to import pygame and this caused an import error
(envpy331)me#ubuntu:~$ python
Python 3.3.1 (default, Apr 19 2013, 11:41:37)
[GCC 4.4.3] on linux
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.virtualenvs/envpy331/lib/python3.3/site-packages/pygame/__init__.py", line 95, in <module>
from pygame.base import *
ImportError: /home/me/.virtualenvs/envpy331/lib/python3.3/site-packages/pygame/base.cpython-33m.so: undefined symbol: PyCObject_FromVoidPtr
Any idea how to correct this?
Pygame has not been entirely ported to Python 3, only some of the modules. If you use Python 2.7 everything should work. I recently had the same problem. Some suggested using individual modules of Pygame with Python 3 but this may be tricky to set up.
I know this is and old question but since I spent some time on the problem, this is the easiest way to install pygame for python3.4 in its own virtual environment:
sudo apt-get build-dep python-pygame
sudo apt-get install python3-dev libswscale-dev libavformat-dev virtualenvwrapper
echo "export WORKON_HOME=~/.venvs" >> ~/.bashrc
echo ". /usr/share/virtualenvwrapper/virtualenvwrapper.sh" >> ~/.bashrc
. ~/.bashrc
The above you do once. The following you repeat for each new environment:
mkvirtualenv -a ~/your/project/path -p /usr/bin/python3.4 your_project_name
pip install hg+http://bitbucket.org/pygame/pygame
This should download and compile the right pygame version (with bunch of warnings, yet working). Tested on Ubuntu GNOME 14.04 and 14.10.
Each time you start working on a given project or wish to switch between projects:
workon your_project_name
The project name will appear in the prompt. And when you want to finish and go back to your system-wide environment:
deactivate