简体   繁体   English

我如何让 tkinter 工作(在 vim 中)以及如何让 python-3.x 作为我的 Linux 的默认 python 运行?

[英]How do I get tkinter to work (in vim) and how do I get python-3.x running as my default python for linux?

From my understanding, tkinter comes pre-installed with python.据我了解,tkinter 预装了 python。 I can't, for the life of my, understand why every IDE I have tried to run tkinter on isn't recognizing tkinter as a library.我一生都无法理解为什么我尝试运行 tkinter 的每个 IDE 都没有将 tkinter 识别为库。

I have tried: import tkinter as tk import Tkinter as tk import * from tkinter from tkinter import * import tkinter import Tkinter and everything else in between...我试过: import tkinter as tk import Tkinter as tk import * from tkinter from tkinter import * import tkinter import Tkinter以及介于两者之间的所有其他内容...

The whole snipit I am trying to run:我试图运行的整个 snipit:

 import Tkinter
 top = Tkinter.Tk()
 top.mainloop()

Whenever I try to run it in VIM the error message is:每当我尝试在 VIM 中运行它时,错误消息是:

Traceback (most recent call last):
  File "tktest.py", line 1, in <module>
    import Tkinter
  File "usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named_tkinter, please install the python-tk package

shell returned 1

Do I need vim to be running python3 in vim (which I thought was default) if so, how do I do that?我是否需要 vim 在 vi​​m 中运行 python3(我认为这是默认设置),如果是这样,我该怎么做?

I have even tried to install tkinter like it says but I get this large error message:我什至尝试像它说的那样安装 tkinter,但我收到了这个大错误消息:

ken@ken-HP-ENVY-Laptop-13-ah1xxx:~$ pip install Tkinter
Collecting Tkinter
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python2.7/dist-packages/pip/req/req_set.py", line 554, in _prepare_file
    require_hashes
  File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python2.7/dist-packages/pip/index.py", line 795, in get_page
    resp.raise_for_status()
  File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/models.py", line 935, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 404 Client Error: Not Found for url: https://pypi.org/simple/tkinter/

It looks like I am running python2.7 on my system by default and I don't know why... how do I get python3.7(or latest version) to be my default for my machine?看起来我默认在我的系统上运行 python2.7,我不知道为什么......我如何让 python3.7(或最新版本)成为我机器的默认值?

For the record, I am using Linux Mint 19.3 Tricia为了记录,我使用的是 Linux Mint 19.3 Tricia

Try running Python 3 in a virtual environment :尝试在虚拟环境中运行 Python 3:

Open a terminal, go to your project directory and run $ python3 -m venv venv which will create a virtual environment called venv in your project directory.打开终端,转到您的项目目录并运行$ python3 -m venv venv这将在您的项目目录中创建一个名为venv的虚拟环境。

To activate it, go to your project directory and run $ source venv/bin/activate要激活它,请转到您的项目目录并运行$ source venv/bin/activate

Your bash prompt should now read (venv) $ and any work you do will take place inside a virtual environment.您的 bash 提示符现在应该显示为(venv) $并且您所做的任何工作都将在虚拟环境中进行。

Once you're done working inside the venv, you want to deactivate it.在 venv 内完成工作后,您想停用它。 To deactivate, type $ deactivate要停用,请键入$ deactivate

NOW, once your virtual environment is active you have a clean, package-free install of Python 3.x where x is whatever version you have installed and aliased to python3 .现在,一旦您的虚拟环境处于活动状态,您就有了一个干净、无包的 Python 3.x 安装,其中 x 是您安装并别名为python3任何版本。

From in here, you can upgrade pip: $ pip install -U pip , re-install all the packages you want from a requirements.txt file: $ pip install -r requirements.txt , and install any other single packages you need with pip as usual.从这里,您可以升级 pip: $ pip install -U pip ,从 requirements.txt 文件重新安装您想要的所有软件包: $ pip install -r requirements.txt ,并使用$ pip install -r requirements.txt您需要的任何其他单个软件包照常。

Anything you install in your virtual environment will only scope into your virtual environment , so you can use this as a test bed, and destroy/rebuild as you see fit.您在虚拟环境中安装的任何内容都只会影响到您的虚拟环境,因此您可以将其用作测试平台,并根据需要销毁/重建。 This is advantageous for a lot of reasons.出于很多原因,这是有利的。


Once you've done this, try launching vim from inside your virtual environment, then try running your code from inside the virtual environment.完成此操作后,尝试从虚拟环境中启动 vim,然后尝试从虚拟环境中运行代码。 Once inside your venv, the only Python your terminal knows about is the one inside the venv.一旦进入你的 venv,你的终端唯一知道的 Python 就是 venv 中的 Python。 So it should default to that one.所以它应该默认为那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM