TKinter in a Virtualenv

前端 未结 9 1713
北荒
北荒 2020-12-05 00:27

Trying to run python code with TKinter-based widgets from a virtualenv.

    user@computer:~/myproject$ env/bin/python Python
    2.7.3 (default, Sep 26 2012,         


        
相关标签:
9条回答
  • 2020-12-05 00:41

    This is really an update to the great answer from A. Rodas for use with Python 3.4 and Tcl 8.6 (I don't have enough reputation points to comment).

    Set the environment variable TCL_LIBRARY in your activate script. On Windows (Python 3.4 with Tcl 8.6), just add this line to Scripts\activate.bat:

    set "TCL_LIBRARY=C:\Python34\tcl\tcl8.6"
    

    I came across this issue while using Windows 7, Python 3.4, and ggplot in a virtual environment.

    0 讨论(0)
  • 2020-12-05 00:44

    I am using python2.7 with a virtualenv on a machine running linux mint. I received the exact same error as mentioned by the OP when running an application that required matplotlib in my virtualenv. "ImportError: No module named _tkinter, please install the python-tk package"

    I ended up deleting and recreating my virtual environment using the suggestions in the above posts. Here are my steps:

    1. if your virtual environment is activated, then run this command to freeze the requirements to a file that you can access later: pip freeze > requirements.txt
    2. if your virtual environment is activated, then deactivate it using: deactivate
    3. delete your virtualenv folder.
    4. install python-tk using: sudo apt-get install python-tk
    5. recreate your virtualenv using: virtualenv <nameofyourenv> --system-site-packages
    6. next, activate your virtualenv: source <virtual environment folder>/bin/activate
    7. restore all your packages that you froze earlier from the requirements.txt file: pip install -r <path to requirements.txt file>

    now, when I run the same code as before, it has no problem importing _tkinter. Hope this helps! Thanks to the everyone's suggestions above. It really helped me a lot.

    0 讨论(0)
  • 2020-12-05 00:51

    In case this helps those scratching their heads after reading through all the answers here - it also seems that you might need to re-create your virtualenv in case you created it before installing python3.6-tk. None of the solutions with specifying TK_PATH and TCL_PATH seemed to work, even when using the paths given by tkinter outside the virtual environment (see this post). To be sure, just delete the venv and create a new one.

    0 讨论(0)
  • 2020-12-05 00:52

    To get this working in powershell, you have to edit the activate.ps1 file instead of activate.bat. Just add the following to activate.ps1: $env:TCL_LIBRARY = "C:\Python27\tcl\tcl8.5"

    0 讨论(0)
  • 2020-12-05 00:53

    For me the solution was copying the folder tcl from

    C:\Users\{myUser}\Python3.5\tcl
    

    to

    C:\Users\{myUser}\{myVirtualEnv}
    

    replace {myUser} and {myVirtualEnv} with your specific directory names.

    0 讨论(0)
  • 2020-12-05 00:54

    Also an update from answer by A.Rodas - I have tried doing that in Windows 7 using Windows Powershell but wasn't able to get it done (I also do not have enough reputation points to comment

    I realized even when I added the line set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5" and the corresponding one for the tk library, to the activate.bat script, the tcl link was not getting updated, and what I needed to do was just go to the directory where it's looking for the tcl files, and copy the corresponding tcl and tk files to that directory. Now the file is in the default location so you don't need to update activate.bat everytime you create a new environment

    0 讨论(0)
提交回复
热议问题