TKinter in a Virtualenv

前端 未结 9 1731
北荒
北荒 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: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 --system-site-packages
    6. next, activate your virtualenv: source /bin/activate
    7. restore all your packages that you froze earlier from the requirements.txt file: pip install -r

    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.

提交回复
热议问题