matplotlib error - no module named tkinter - python

I tried to use the matplotlib package via Pycharm IDE on windows 10.
when I run this code:
from matplotlib import pyplot
I get the following error:
ImportError: No module named 'tkinter'
I know that in python 2.x it was called Tkinter, but that is not the problem - I just installed a brand new python 3.5.1.
EDIT: in addition, I also tried to import 'tkinter' and 'Tkinter' - neither of these worked (both returned the error message I mentioned).

For Linux
Debian based distros:
sudo apt-get install python3-tk
RPM based distros:
sudo yum install python3-tkinter
For windows:
For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html . Good python distributions for Windows can be found by the companies Anaconda or ActiveState.
Test the python module
python -c "import tkinter"
p.s. I suggest installing ipython, which provides powerful shell and necessary packages as well.

you can use
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
if you dont want to use tkinter at all.
Also dont forget to use %matplotlib inline at the top of your notebook if using one.
EDIT: agg is a different backend like tkinter for matplotlib.

For Windows users, there's no need to download the installer again. Just do the following:
Go to start menu, type Apps & features,
Search for "python" in the search box,
Select the Python version (e.g. Python 3.8.3rc1(32-bit)) and click Modify,
On the Modify Setup page click Modify,
Tick td/tk and IDLE checkbox (which installs tkinter) and click next.
Wait for installation and you're done.

On Centos, the package names and commands are different. You'll need to do:
sudo yum install tkinter
To fix the problem.

Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:
import tkinter
import _tkinter
tkinter._test()

If you are using fedora then first install tkinter
sudo dnf install python3-tkinter
I don't think you need to import tkinter afterwards
I also suggest you to use virtualenv
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And add the necessary packages using pip

On CentOS 7 and Python 3.4, the command is sudo yum install python34-tkinter
On Redhat 7.4 with Python 3.6, the command is sudo yum install rh-python36-python-tkinter

For windows users, re-run the installer. Select Modify. Check the box for tcl/tk and IDLE. The description for this says "Installs tkinter"

On Ubuntu, early 2018, there is no python3.6-tk on ubuntu's (xenial/16.04) normal distributions, so even if you have earlier versions of python-tk this won't work.
My solution was to use set everything up with python 3.5:
sudo apt install python3.5-tk
virtualenv --python=`which python3.5` python-env
source python-env/bin/activate
pip install -r requirements.txt
And now matplotlib can find tkinter.
EDIT:
I just needed 3.6 afterall, and the trick was to:
sudo apt install tk-dev
and then rebuild python3.6, after tk-dev, eg:
./configure
make
make install

If you are using python 3.6, this worked for me:
sudo apt-get install python3.6-tk
instead of
sudo apt-get install python3-tk
Which works for other versions of python3

For the poor guys like me using python 3.7. You need the python3.7-tk package.
sudo apt install python3.7-tk
$ python
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()
Note. python3-tk is installed. But not python3.7-tk.
$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
tix python3.7-tk-dbg
The following NEW packages will be installed:
python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...
After installing it, all good.
$ python3
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()

On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter

Sometimes (for example in osgeo4w distribution) tkinter is removed.
Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

Since I'm using Python 3.7 on Ubuntu I had to use:
sudo apt-get install python3.7-tk

Maybe you installed python from source. In this case, you can recompile python with tcl/tk supported.
Complie and install tcl/tk from http://www.tcl.tk/software/tcltk/download.html, I'll suppose you installed python at /home/xxx/local/tcl-tk/.
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
Recompile python with tcl/tk supported, for example:
# download the source code of python and decompress it first.
cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
--with-tcltk-includes=/home/xxx/local/tcl-tk/include \
--with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make
make install

I had the same issue on Win x86/64 because my custom Python3.7 installation did not include Tcl packages, so just modify or re-install your python
https://www.python.org/downloads/release/python-370/

Download Python Setup file and click modify then tick the tcl/tk and install.
After installation is complete go to folder where python is installed ( Default is C:\Users*Your username*\AppData\Local\Programs\Python\Python39\Lib) .
Copy the tkinter folder and paste it in the lib folder of your pycharm project.
Error should be resolved

Follow these steps to easily install Tkinter on your PyCharm IDE:
First go to the File:
Second is go to New Project Setup > Settings for new projects:
And then click the Settings for new projects and you'll get redirected in here:
Please click the + symbol in there:
after that install future and there you go...

If you’re having pip(which you probably do), open up cmd or powershell on Windows or a terminal window on OS X or Linux and try this(make sure python is in the system path if you’re on Windows):
pip install tkinter
It should take a while to install tkinter, and then try to execute this code block:
from tkinter import *
root = Tk()
# Your code goes here
root.mainloop()
Hope that this helps! Thank you!

Related

python scripts folder is empty

pip was selected while installing ,but when i checked scripts folder ,the folder was empty
since scripts are missing i cannot use pip cammand on terminal,new to programming please help me!!!
i tried installing pip this particular error showes up!!
FOR WINDOWS ONLY
open cmd.
then type pip help, if pip is installed then a message would come explaining how to
use it else an error message stating that the program is not found.
then type python and see whether that's installed properly , if you receive this
Python is not recognized as an internal or external command, operable program or batch file then this means that python is not installed on your system and you need to
reinstall it. Or if it is installed properly then you would get something like this
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
to install pip download this pip install and then open your cmd in that folder
where you have installed it by using the cd command(like if you have installed it in downloads you should type in the cmd cd Downloads), then type this python get-pip.py.
And this should have installed pip on your system . (see step 1 to see if it works)
Then just upgrade pip and you are good to go , you can upgrade pip by python -m pip install --upgrade pip
Then try installing a module like pip install tksplash in the cmd.
FOR LINUX ONLY
open your terminal and type sudo apt install python3-pip(works only on debian based disrtos like -: Ubuntu ,Kali , Parrot))
you have not install any pip packages.
you can install new one, then refresh dir.
pip install Flask
Installing Anaconda will be better option. It has all packages required.
get-pip.py needs to be downloaded and run python get-pip.py.
Use Python 3.10, it installs pip properly out of the box.

How do I download Pygame for Python 3.5.1?

I am unable to find a pygame download for Python 3.5 and the ones I have downloaded don't seem to work when I import to the shell. Help?
This is the message I receive on the shell:
import pygame
Traceback (most recent call last):
File "", line 1, in
import pygame
ImportError: No module named 'pygame'
I'm gonna guess your using Windows. If you are not then there is no special version of pygame for Python 3+. If you do have Windows then read below.
You will need pygame to be part of your path to do this. This is so you can use this in the command prompt. Make sure you use it as an admin when doing this.
First you need to find out what bit version of Python you have. Open your Python shell and at the top of the window it should say something like "Pygame V(some number) (bit number)" You want the bit number.
Now you ned to open the command prompt. Use the "windows key + r key" to open the run menu, and type "cmd" and press enter. Or you can just search your PC for "cmd" and right click on it and select "run as admin" to open as an admin.
Python comes with a special path command called "pip." I am not gonna get into this module too much, but in short it is used to install addition Python modules. The first thing you need to do is this command...
pip install wheel
The screen should print some stuff off while doing this. You can tell if the module installed correctly because it should print something like "wheel installed successfully." We are gonna need this later.
Now you need to get your pygame file. Go here and find the pygame section. If you have python 32 bit download you should download this "pygame-1.9.2b1-cp35-cp35m-win32.whl" or if you have 64 bit Python download "pygame-1.9.2b1-cp35-cp35m-win_amd64.whl". I am pretty sure these are the ones you need for your bit version, but I installed pygame on my Windows 10 a few months ago so they may be different now.
Once you have this downloaded go back to the command prompt. Enter this command...
pip install (filename)
Make sure it includes the .whl extension. If you get an error then specify the path to the folder the file is in (which should be the downloads folder). Once again you should see a message similar to "pygame installed successfully."
Once all this is done open your Python shell and type...
import pygame
If it works you now have pygame available for use. If not then there are a few more things you can try...
Try restarting your PC. Sometimes these things don't take affect until a system restart.
Try installing a different version of pygame from the website listed above. It may just be a simple issue due to bit version differences.
Make sure you actually installed the pygame module from the file. It may of thrown an error that appeared to be an actual successful installation. It always pays to double-check.
Like I said before I installed pygame on my Windows 10 with Python 3.4 64 bit a few months ago in the same way I told you here so it should work, but may be outdated. Anyways I hope this helps you with your pygame installation issues and the best of luck to you!
For windows now you simply use pip as it's available directly to install as pygame.
Use the following command:
python -m pip install pygame
It should output something like this, then you can test if it's working by importing pygame.
PS C:\Windows\system32> python -m pip install pygame
Collecting pygame
Downloading pygame-1.9.2b1-cp35-cp35m-win32.whl (4.4MB)
100% |################################| 4.4MB 264kB/s
Installing collected packages: pygame
Successfully installed pygame-1.9.2b1
PS C:\Windows\system32> python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
# Note there is no error here...
>>> quit()
PS C:\Windows\system32>
This worked preety well for me:
System: Ubuntu 16.10 x64
root#sonic-VirtualBox:~/python# cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.10
DISTRIB_CODENAME=yakkety
DISTRIB_DESCRIPTION="Ubuntu 16.10"
NAME="Ubuntu"
VERSION="16.10 (Yakkety Yak)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.10"
VERSION_ID="16.10"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="http://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=yakkety
UBUNTU_CODENAME=yakkety
root#sonic-VirtualBox:~/python#
Python 2.7 + Pygame:
root#sonic-VirtualBox:~/python# python -V
Python 2.7.12+
# apt-get install python-pip
# pip install --upgrade pip
# pip install pygame
Python 3.5 + Pygame:
(Python 3.5.2+ in my case)
root#sonic-VirtualBox:~/python# python3 -V
Python 3.5.2+
# sudo apt-get install python3-pip
# pip3 install --upgrade pip
# pip3 install pygame
Download to pygame3.5
Then put this into your cmd
cd [location of python3.5]
python -m pip install [location of pygame]
python -m pip install --upgrade pip
Then type
import pygame
pygame.init()
Even when you have windows 64 bit you need to get the win32.whl file , then follow the standard instructions
In an anaconda environment with python 3.5 installed, you can simply do:
pip install pygame
$pip install pygame
Collecting pygame
Downloading pygame-1.9.3-cp35-cp35m-manylinux1_x86_64.whl (9.4MB)
100% |***************************| 9.4MB 132kB/s
Installing collected packages: pygame
Successfully installed pygame-1.9.3
I'm in ubuntu 14.04, this should work with newer and other linuxes.

How to properly install wxPython?

So I was looking around at different things to do on Python, like code for flashing text or a timer, but when I copied them into my window, there were constant syntax errors. Now, maybe you're not meant to copy them straight in, but one error I got was 'no module named wx'. I learned that I could get that module by installing wxPython. Problem is, I've tried all 4 options and none of them have worked for me. Which one do I download and how do I set it up using Windows?
Thanks
It's on PyPI. As of wxPython 4, Python 3 is supported.
Unfortunately, PyPI has a package called wx that is stuck at version 3.0.3; be sure to install the package named wxpython instead.
pip install wxpython
Please note that pip will automatically build wxWidgets for you, but it will not install wxWidgets system dependencies such as GTK and OpenGLu. If the above command exits with an error, look above for a message like this:
checking for <something>... not found
checking for <something>... no
configure: error: <prereq> libraries not available
Error running configure
ERROR: failed building widgets
This should give you information about at least one of the packages your system is missing.
The "official" list of prerequisites from the wxWidgets source is:
dpkg-dev
build-essential
libjpeg-dev
libtiff-dev
libsdl1.2-dev
libgstreamer-plugins-base0.10-dev # or 1.0 if available
libnotify-dev
freeglut3
freeglut3-dev
libsm-dev
libgtk-3-dev
libwebkitgtk-3.0-dev # or libwebkit2gtk-4.0-dev if available
libxtst-dev
The actual package names provided by your package manager may not match these exactly, and to be honest, I don't really know the best way to query a package manager to determine what packages provide the libraries you need.
3 steps to install wx-widgets and pygame in python IDLE
Install python 3xxx in your system opting (Add 3xxx to your path).
open python CLI to see whether python is working or not.
then open command prompt (CMD).
type PIP to see whether pip is installed or not.
enter command : pip install wheel
enter command : pip install pygame
To install wxpython
enter command : pip install -U wxPython
Thats all !!
As per home page instruction:
Make sure you have at least version 6.0.8 of pip and 12.0.5 for setuptools.
Install requirements for Linux as outlined in the readme.rst at:
https://github.com/wxWidgets/Phoenix/blob/master/README.rst
Install wxPython-Phoenix (Linux):
sudo pip install --upgrade --trusted-host wxpython.org --pre -f http://wxpython.org/Phoenix/snapshot-builds/ wxPython_Phoenix
Install wxPython-Phoenix (Windows, use the appropriate script folder):
C:\python27\scripts\pip.exe install --upgrade --trusted-host wxpython.org --pre -f http://wxpython.org/Phoenix/snapshot-builds/ wxPython_Phoenix
I installed wxPython as part of the PsychoPy experiment builder dependencies, and had considerable trouble getting it to install properly as well initially. But this was what worked for me at the end. I use Ubuntu 16.04, python 3.5, pip3 19.0.3
pip3 install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython --user
If you use Conda then you may easily setup the environment with wx by one line:
$ conda create -n wxenv python=3 wxPython
Solving environment: done
## Package Plan ##
environment location: /home/user/.conda/envs/wxenv
added / updated specs:
- python=3
- wxpython
The following packages will be downloaded:
package | build
---------------------------|-----------------
[...]
Proceed ([y]/n)?
You need to ensure the versions of your wxPython download matches your installed python language library.
The current downloads wxPython downloads doesn't show any libraries built against python 3. I Believe the python 3 porting project is still ongoing.
If you are not sure of what you are doing I would stick with the 32bit version on windows as there are some Python libraries (ie IIRC, MySQLdb) which don't work with 64 bit python.
So you would then need to download python2.7 for windows x86 and "wxPython3.0-win32-py27 32-bit Python 2.7"
To install wxPython GUI library correctly go to the following page (https://wxpython.org/Phoenix/snapshot-builds/), which contains snapshots builds of wxPython library (Phoenix version) depending on your os and version of Python you want to work.
Then when you downloaded the proper package for your system and python version, simply install it by using pip. In my case I've choosen that one (wxPython_Phoenix-3.0.3.dev2811+ecc4797-cp36-cp36m-win_amd64.whl):
pip install wxPython_Phoenix-3.0.3.dev2811+ecc4797-cp36-cp36m-win_amd64.whl
To check that it has been installed sucessfully on the site-packages folder for your current python environment write:
pip freeze
It's all!
Check the version of wxpython and the version of python you have in your machine.
For python 2.7 use wxPython3.0-win32-3.0.2.0-py27 package
The problem was solved in openSuse simply with
zypper in python-wxWidgets-3_0-devel
Trying pip install before, gave me a lot of trouble (missing traits, missing wx/setup.h, https://github.com/wxWidgets/Phoenix/issues/1644, error: aggregate ‘wxGLAttributes _NullGLAttributes’ has incomplete type and cannot be defined, etc.).
wxpython failed to be installed with pipenv. Pipenv is not able to find wxpython binary so it tries to build wxpython but fails.
CXXFLAGS="-I/opt/homebrew/include" pipenv install wxpython
On my macOS M1 pipenv failed to install wxPython. After a lot of searching I found a forum post which really helped me fix the problem.
Source/Credits: https://forums.wxwidgets.org/viewtopic.php?t=47953&p=203709
Install current development version with:
pip install -U https://github.com/robotframework/RIDE/archive/master.zip
(python < 3.9) Install current Beta version (2.0b1) with:
pip install psutil
pip install -U --pre robotframework-ride
Note that I tried to install wxPython with 'pip install -U wxPython' as per instruction
with no avail. Too many errors to list here. 🤨
I found a solution to the problem!!
I'm working on a 64b machine and Windows 11 operating system using VSCode.
Here is the solution using PowerShell:
Version specs:
pip 22.3.1
virtualenv 20.15.1
python 3.10.8
Create a new virtual environment in the directory where the program resides and
activate. There must be no modules installed.
virtualenv venv
venv/scripts/activate.bat
Install the following in sequence:
pip install pygame
(Not sure why pygame must be installed first, but this was
recommended and it works) 😟
pip install -U wxPython
SUCCESS!!! 🤠
These are the modules installed:
numpy 1.24.1
Pillow 9.4.0
pip 22.3.1
pygame 2.1.2
setuptools 65.4.0
six 1.16.0
wheel 0.37.1
wxPython 4.2.0
VSCode still reports wx as a missing module even when you activate the virtual
environment within. Running the code from the PS command prompt within the virtual
environment is the only working solution.
PS. I am sure there are some conflicts when trying to install wxPython within an
environment where all the other modules are installed.

Installing VTK for Python

I'm trying to install VTK module for python, I am however unsuccesful in doing so. I have downloaded a VTK tar-file, but I'm unable to extract it. I'm capable of extracting other tar-files, so there must be something specific with this file I suppose.
This is my error:
gzip: stdin: invalid compressed data--format violated
tar: Child returned status 1
tar: Error is not recoverable: exiting now
I hope somebody can help me with this.
The answer depends on the operating system you are using. This will be a lot easier if you can find a package or installer for your specific operating system and/or distribution.
Linux
If you are using Linux then look for the corresponding package in the distribution's package manager. For example, on Ubuntu Linux you should be able to install it using the following command:
sudo apt-get install python-vtk
Microsoft Windows
If you are using Microsoft Windows, the easiest way would be to install Python(x,y). It comes with VTK support.
Additionally, Anaconda also includes VTK package as well as support for virtual environments. It might be a good option for some folks.
Mac OS X
If you are using Mac OS X, try installing everything via MacPorts.
As #Nil mentioned in comments below, a standalone python interface to VTK is now provided by VTK developers. You may download it for Windows, Darwin, and Linux from here.
As mentioned by #Nil, VTK used to offer vtkpython binaries on their download page. However, they've dropped this since VTK-8.x.x as mentioned here:
Sorry, about that. We decided to drop the vtkpython binaries for 8. I want to focus our energies on supporting python wheel installs instead. There’s no timeline yet for a complete solution but we’ve made some good progress toward that recently here: https://github.com/jcfr/VTKPythonPackage.
Thus, the recommended way of installing vtkpython now is (see this page):
$ python -m pip install --upgrade pip
$ python -m pip install vtk
on Ubuntu, maybe this post will be helpful:
http://kazenotaiyo.blogspot.jp/2010/06/installing-vtk-in-ubuntu-and-making.html
The easiest way
The first and easiest is to just install the packages with the Aptitude Package Manager:
sudo apt-get install libvtk5-dev python-vtk
If you want the newest version
If you want the newest version VTK, you can also build it yourself:
Make sure CMake is installed:
sudo apt-get install cmake
Download the VTK source from the Downloads page.
Untar it:
tar xvzf vtk-5.6.0.tar.gz
Create an Out-Of-Source build and configure with CMake:
mkdir VTK_BUILD
cd VTK_BUILD
ccmake ../VTK
Make sure you enable python wrapping and set your install prefix to where you want the package to go. The default /usr/local works fine.
sudo make -j 8 install
(the -j 8 for make just makes the build process parallel assuming you've got the processors for it)
You now have VTK installed. Congrats! if you try to run vtkpython though, you'll get an error:
vtkpython: error while loading shared libraries: libvtksys.so.5.6: cannot open shared object file: No such file or directory
To fix this, append these lines to your .bash_profile, .bashrc, or .profile file in your home directory:
# add vtk paths
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib/vtk-5.6"
PYTHONPATH="$PYTHONPATH:/usr/local/lib/vtk-5.6"
You'll need to reset your terminal now.
That sets up your library and python paths for the vtkpython executable.
http://www.lfd.uci.edu/~gohlke/pythonlibs/#vtk Try this! Works for windows !
I have installed vtk without a problem under win7 via pip:
> pip install vtk
Collecting vtk
Downloading vtk-8.1.0-cp36-cp36m-win_amd64.whl (24.4MB)
100% |████████████████████████████████| 24.4MB 56kB/s
Installing collected packages: vtk
Successfully installed vtk-8.1.0
With Anacond python:
> python
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
MacOS only:
See How to Install Mayavi on MacOS
An abridged version of this for MacOS is the following: (but I recommend the full procedure based on above link, but note that it installs Mayavi too)
The following steps seem to work on MacOS:
brew install vtk
pip install vtk
This installs vtk#9.1. You may want to install brew install vtk#8.2 instead.
Tested on:
Python: 3.9.13, MacOS: 12.4 Monterey
PS. As mentioned before, this answer may be incomplete (you may need QT too, bu tI am not sure). For a complete one including Mayavi, see My answer here . I suggest following the steps there.
I didn't update all content s here because I am not sure which steps are required if you only need VTK (not Mayavi). For example, I don't know whether you need QT or not.

Python unable to find lxml module

I wrote a script some times ago that contain
from lxml import etree
But, unfortunatly it is not working anymore.
In doubt i checked installation with :
sudo apt-get install python-lxml
sudo pip install lxml
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
I checked if it could be my python version with :
me#pc:~$ python
Python 2.7.3 (default, Sep 14 2012, 14:11:57)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named lxml
My os is ubuntu 12.04.1 LTS with Python 2.7.3.
All seems fine. I can't see what could be the problem.
Your solution cited in edit, which use the xml.etree instead of lxml.etree is not the better way to do it, as these module have known incompatibilities, and mainly because lxml is certainly more optimised.
A good way to make a clean environment available is to use virtualenv :
$ virtualenv myproject
$ cd myproject
$ ./bin/pip install lxml # Repeat this with other dependencies
[wait for download and compiling]
Then, use ./bin/python to execute your script.
The advantages of this method are :
you can have different versions of dependencies between your system and your project
even if you break everything in your virtual environment, you will not compromised the rest of your system
you do not need root privileges to make an installation
As a reference, a more powerful but slightly more complex way to achieve this is to use buildout, but it can looks like hunting flies with a bazooka if you just want to execute a simple one-file script.
Solved the problem.
It seems that a software that i installed messed out my python path. The python that i was using when calling python in a terminal was the one installed by the software, and the one called by my script was the one installed on my system.
So, i just removed the python path of the software from the path variable of bash.
I had this problem when running tests in PyCharm. I could import lxml when using the terminal and virtual environment, but using the same virtual environment to run the tests resulted in the ImportError.
In my case upgrading from lxml 3.3.5 to 4.2.5 fixed the problem.
This likely has something do to with your Python environment. Here are three troubleshooting methods, from least to most cumbersome
1) Re-install via pip
pip uninstall lxml
pip install lxml
2) Re-Install via IDE
uninstalling it from the terminal (pip uninstall lxml)
adding from lxml import etree at the beginning of a python file
letting my IDE (PyCharm) show a missing import error
hovering over the error and installing it from the IDE via the error-fixing pop-up (not terminal)
3) Re-create virtualenv
Assuming you're working in a virtual environment (if not, shame to you and your family): delete and create a new environment.
if youre dealing with conda, i made the "discovery, that my conda install lxml does not work propperly after "successfully" installing
try uninstalling and reinstalling with pip, that worked for me
conda remove lxml
pip install lxml
For Python 3.6, it is very tricky.
I have the same problem and try to install the latest version, lxml 3.8. But I still get the error. And then I use lxml 3.7.3, then it is ok.

Categories

Resources