TKinter in a Virtualenv

前端 未结 9 1714
北荒
北荒 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:59

    I manage to integrate tkinter in python3 to virtualenv by symlink tkinter folder to virtualenv python3. I works for me. But I don't know if it's the right way.

    1. install tkinter
    sudo apt-get install python3-tk
    
    1. go to your virtualenv's python lib folder
    cd ~/.virtualenvs/cv/lib/python3.4/
    
    1. link the tkinter
    ln -s /usr/lib/python3.4/tkinter tkinter
    

    Hope this helps.

    In later versions of python, this may result in a

    ModuleNotFoundError: No module named '_tkinter'
    

    In this case, ensure to also symlink
    /usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so
    as path/to/virtualenv/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so using

    ln -s /usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so _tkinter.cpython-36m-x86_64-linux-gnu.so
    

    from within your virtualenv lib/python3.x/lib-dynload/ directory.

    0 讨论(0)
  • 2020-12-05 01:05

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

    set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
    

    @Jasper van den Bosch's edit: On Ubuntu, the modification to the script activate is the following:

    TK_LIBRARY=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib
    TKPATH=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib 
    TCL_LIBRARY=/usr/lib 
    export TCL_LIBRARY TK_LIBRARY TKPATH
    

    The reference of this can be found on this question on askubuntu

    0 讨论(0)
  • 2020-12-05 01:06

    clone the cpython project

    git clone git@github.com:python/cpython.git
    

    cd to the cpython directory. git checkout the desired version of your virtual env(for me it is 3.4), and build it with

    ./configure
    make
    make test
    sudo make install
    

    you will find an so file _tkinter.cpython-xxx.so in a subdir of the build/ directory, copy it to your venv's lib-dynload dir. (for me it is ~/tf1.1py3.4/lib/python3.4/lib-dynload/)

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