poetry: Can't use tkinter in virtual environment created by poetry
- OS version and name: Ubuntu 20.04
- Poetry version: 1.1.7
- Link of a Gist with the contents of your pyproject.toml file: https://gist.github.com/vitalije/77d16343bf8f0a99ec0946f2586edf5b
Issue
I have several python installations. Using one that I have compiled myself (*Python 3.9.6) with support of latest stable tk/tcl (8.6.11). This python when invoked directly has full support for tkinter library. However, when I create new virtual environment using:
poetry env use /path/to/my/python3.9.6
poetry shell
python -m tkinter
I get the error that python was not configured to use tkinter module.
When I manually add links lib/tcl8.6
and lib/tk8.6
to the virtual environment in ~/.cache/pypoetry/virtualenvs/mleo.../lib/
, python can use tkinter afterwards.
I was expecting that poetry would add those links when creating new virtual environment.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 18 (6 by maintainers)
If you use
pyenv
to manage your Python versions (which you should), you’ll run into this issue. I was able to fix it by following these two steps.Step 1: Installing Necessary System Packages
Using
tkinter
withpyenv
can be tricky due to missing dependencies.First, let’s make sure we have installed the necessary system packages for
tkinter
.In most Linux systems, you can install them with the following commands:
On macOS, you can use Homebrew to achieve the same:
Step 2: Linking the Correct Tcl/Tk Versions
Next, make sure you link the correct versions of Tcl/Tk when installing Python.
pyenv
builds Python in your environment, but if you don’t have the required dependencies, like the Tk/Tcl libraries, it’ll build Python without them.If the Python version you want to use is already installed on your system, you’ll need to uninstall it before proceeding. For example:
When installing a new Python version with
pyenv
, use the following commands:For Linux:
For macOS, after installing tcl-tk with brew:
Replace
<version>
with the version of Python you want to install. After that, you should be able to importtkinter
in yourpyenv
Python environment.Note that I couldn’t get
tkinter
to work with pyenv
on Python 3.11.0
, but it worked perfectly on3.11.2
.I too have had this issue. After some testing, it seems fixed with the current version of Poetry. Try reinstalling it. Here are some fixes…
poetry env info -p
for the path to your virtualenvvirtualenv = 20.*.*
I hope this helped you. I know it is a bit late, but hopefully someone else stumbles upon this and it fixes their issue. 😄
My working version of pyvenv.cfg…
@djeikyb the issue you are facing is not a poetry issue IMHO.
This would seem that you didn’t have the required system dependencies available for the version of python used for your project virtual environment.
Once the dependencies are installed and available you can specify the use of the correct python version for your virtual environment.
If you want to use the system site installed package, you can also attempt to use https://python-poetry.org/docs/configuration/#virtualenvsoptionssystem-site-packages. However, that is also bound to the site of the python version being used.
Ok, I tracked this down to poetry seemingly ignoring
poetry env use /path/to/python
in some cases. That is, you can have the following (pseudo-bash):Perhaps you have to
poetry env use /path/to/python
before even creating the environment? If so, it’s incredibly misleading thatpoetry env use
is a no-op once the environment has been created.