在python中在atom中添加模块 [英] Adding modules in atom in python

查看:45
本文介绍了在python中在atom中添加模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机上安装了模块 pygame 和 tkinter,但我无法找到如何在文本编辑器 atom 中访问它们.我可以通过使用

I have the module pygame and tkinter installed on my computer, but I cannot find out how to access them in the text editor atom. I can use them in IDLE by using

import pygame 

但是当我尝试在 atom 中导入它时,它无法识别该模块.有人知道为什么吗?我的所有其他导入都可以正常工作(时间、随机等),但我认为它们可以工作,因为它们是随 Python 一起安装的(对吗?).

but when I try to import it in atom it doesn't recognize the module. Anybody know why? All of my other imports work by the way (time, random, etc.) but I think that they work because they come installed with python (right?).

推荐答案

首先,atom 是一个文本编辑器,它本身不运行 python 代码.所以当你说你试图在 atom 中导入它"时,我必须假设你正在使用一个可以解释代码的 atom 插件.一个常见的插件是脚本".

Firstly, atom is a text editor, it does not run python code by itself. So when you say you're trying to "import it in atom", I have to assume you're using an atom plugin that can interpret code. One common plugin for this is "script".

您需要前往 atom 设置来检查正在使用哪个 python 安装.首先使用 control+, 输入原子设置,然后单击导航器上的包",然后单击脚本"(或您用来运行代码的任何插件,例如脚本,氢、原子流道等).在脚本设置中,点击查看代码",将打开脚本的源代码.在左侧的树导航器上,导航到 script-->lib-->grammars-->python.coffee

You need to head to the atom settings to check which python install is being used. Do this by first entering atom settings with control+,, then click on "packages" on the navagator, and click on "script" (or whatever plugin you're using to run your code, e.g. script, Hydrogen, atom-runner etc.). In the script settings, click on "view code", which will open the source code of script. On the tree nagivator on the left, navigate to script-->lib-->grammars-->python.coffee

在这个 coffeescript 文件中,你应该找到 atom-script 使用的 python 解释器.例如,由于我使用 anaconda,我的内容如下:

In this coffeescript file, you should find the python interpreter that atom-script is using. For example, since I use anaconda, mine reads:

exports.Python =
  'Selection Based':
    command: '/home/username/.miniconda3/envs/local35/bin/python'
    args: (context) -> ['-u', '-c', context.getCode()]

如果你运行的是 linux 或 mac,现在你可以打开一个终端并运行

If you run linux or mac, now you can open a terminal and run

which python

import pygame 如果系统 python 匹配正在使用的一个原子,理论上应该可以工作,所以这些可能会有所不同.只需将coffeescript 文件中的python 路径替换为which python 的结果.如果这些已经相同,则此过程至少应该为您提供一些线索以进一步诊断问题.

import pygame should theoretically work if the system python matches the one atom is using, so these will probably be different. Just replace the python path in the coffeescript file with the result of which python. If these are already the same, this process should at least give you some clues to further diagnose the issue.

就我个人而言,我建议使用 Anaconda 来跟踪您的 Python 环境和模块,它确实可以帮助解决此类问题.

Personally, I recommend the use of Anaconda to keep track of your python environments and modules, it can really help with issues like this.

还要确保 python -c "import pygame" 有效,因为如果无效,这根本就不是原子问题.

Also do make sure that python -c "import pygame" works, because if it doesn't, this isn't an atom issue at all.

这篇关于在python中在atom中添加模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆