Adding modules in atom in python

∥☆過路亽.° 提交于 2020-01-25 07:58:08

问题


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 

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?).


回答1:


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".

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

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()]

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

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.

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

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




回答2:


I'm a new user. John Kealy's answer is what helped solve my Atom 1.40.1 (using Script 3.18.1,) "..module not found" issue. From the Python 3.7 interpreter:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32

My pip list:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\erica> pip list
Package         Version
--------------- -----------
bleak           0.5.0
bricknil-bleak  0.3.1
cycler          0.10.0
kiwisolver      1.1.0
matplotlib      3.1.1
mpmath          1.1.0
numpy           1.16.4
pip             19.2.3
pybluez         0.22
pyparsing       2.4.0
python-dateutil 2.8.0
pythonnet       2.4.0
pyVIP           1.3.0
scipy           1.3.0
setuptools      41.2.0
six             1.12.0
sympy           1.4
virtualenv      16.7.4
wheel           0.33.4
winrt           1.0.19128.1

I right-clicked on the Python interpreter icon and copied the target string from the properties dialog box:

C:\Users\erica\AppData\Local\Programs\Python\Python37\python.exe

From the python.coffee script John mentioned, where "command:" originally read:

exports.Python =
    'Selection Based':
      command: 'python'
      args: (context) -> ['-u', '-c', context.getCode()]

    'File Based':
      command: 'python'
      args: ({filepath}) -> ['-u', filepath]

I then modified to:

exports.Python =
'Selection Based':
  command: 'C:/Users/erica/AppData/Local/Programs/Python/Python37/python'
  args: (context) -> ['-u', '-c', context.getCode()]

'File Based':
  command: 'C:/Users/erica/AppData/Local/Programs/Python/Python37/python'
  args: ({filepath}) -> ['-u', filepath]

So now I can have this code in my test.py file:

import bluetooth as bt
for e in bt.__dict__:
    print(e)

...and cntrl-shift-b to the results in the Script output window at the bottom of my Atom screen:

[Command: C:/Users/erica/AppData/Local/Programs/Python/Python37/python -u     C:\Users\erica\AppData\Local\Programs\Python\Python37\test.py]
__name__
__doc__
__package__
__loader__
__spec__
__path__
__builtins__
__file__
__cached__
sys
os
btcommon
struct
binascii
L2CAP
RFCOMM
PORT_ANY
SDP_SERVER_CLASS
BROWSE_GRP_DESC_CLASS
PUBLIC_BROWSE_GROUP
SERIAL_PORT_CLASS
LAN_ACCESS_CLASS
DIALUP_NET_CLASS
IRMC_SYNC_CLASS
OBEX_OBJPUSH_CLASS
OBEX_FILETRANS_CLASS
IRMC_SYNC_CMD_CLASS
HEADSET_CLASS
CORDLESS_TELEPHONY_CLASS
AUDIO_SOURCE_CLASS
AUDIO_SINK_CLASS
AV_REMOTE_TARGET_CLASS
ADVANCED_AUDIO_CLASS
AV_REMOTE_CLASS
VIDEO_CONF_CLASS
INTERCOM_CLASS
FAX_CLASS
HEADSET_AGW_CLASS
WAP_CLASS
WAP_CLIENT_CLASS
PANU_CLASS
NAP_CLASS
GN_CLASS
DIRECT_PRINTING_CLASS
REFERENCE_PRINTING_CLASS
IMAGING_CLASS
IMAGING_RESPONDER_CLASS
IMAGING_ARCHIVE_CLASS
IMAGING_REFOBJS_CLASS
HANDSFREE_CLASS
HANDSFREE_AGW_CLASS
DIRECT_PRT_REFOBJS_CLASS
REFLECTED_UI_CLASS
BASIC_PRINTING_CLASS
PRINTING_STATUS_CLASS
HID_CLASS
HCR_CLASS
HCR_PRINT_CLASS
HCR_SCAN_CLASS
CIP_CLASS
VIDEO_CONF_GW_CLASS
UDI_MT_CLASS
UDI_TA_CLASS
AV_CLASS
SAP_CLASS
PNP_INFO_CLASS
GENERIC_NETWORKING_CLASS
GENERIC_FILETRANS_CLASS
GENERIC_AUDIO_CLASS
GENERIC_TELEPHONY_CLASS
UPNP_CLASS
UPNP_IP_CLASS
UPNP_PAN_CLASS
UPNP_LAP_CLASS
UPNP_L2CAP_CLASS
VIDEO_SOURCE_CLASS
VIDEO_SINK_CLASS
SDP_SERVER_PROFILE
BROWSE_GRP_DESC_PROFILE
SERIAL_PORT_PROFILE
LAN_ACCESS_PROFILE
DIALUP_NET_PROFILE
IRMC_SYNC_PROFILE
OBEX_OBJPUSH_PROFILE
OBEX_FILETRANS_PROFILE
IRMC_SYNC_CMD_PROFILE
HEADSET_PROFILE
CORDLESS_TELEPHONY_PROFILE
AUDIO_SOURCE_PROFILE
AUDIO_SINK_PROFILE
AV_REMOTE_TARGET_PROFILE
ADVANCED_AUDIO_PROFILE
AV_REMOTE_PROFILE
VIDEO_CONF_PROFILE
INTERCOM_PROFILE
FAX_PROFILE
HEADSET_AGW_PROFILE
WAP_PROFILE
WAP_CLIENT_PROFILE
PANU_PROFILE
NAP_PROFILE
GN_PROFILE
DIRECT_PRINTING_PROFILE
REFERENCE_PRINTING_PROFILE
IMAGING_PROFILE
IMAGING_RESPONDER_PROFILE
IMAGING_ARCHIVE_PROFILE
IMAGING_REFOBJS_PROFILE
HANDSFREE_PROFILE
HANDSFREE_AGW_PROFILE
DIRECT_PRT_REFOBJS_PROFILE
REFLECTED_UI_PROFILE
BASIC_PRINTING_PROFILE
PRINTING_STATUS_PROFILE
HID_PROFILE
HCR_PROFILE
HCR_PRINT_PROFILE
HCR_SCAN_PROFILE
CIP_PROFILE
VIDEO_CONF_GW_PROFILE
UDI_MT_PROFILE
UDI_TA_PROFILE
AV_PROFILE
SAP_PROFILE
PNP_INFO_PROFILE
GENERIC_NETWORKING_PROFILE
GENERIC_FILETRANS_PROFILE
GENERIC_AUDIO_PROFILE
GENERIC_TELEPHONY_PROFILE
UPNP_PROFILE
UPNP_IP_PROFILE
UPNP_PAN_PROFILE
UPNP_LAP_PROFILE
UPNP_L2CAP_PROFILE
VIDEO_SOURCE_PROFILE
VIDEO_SINK_PROFILE
SERVICE_RECORD_HANDLE_ATTRID
SERVICE_CLASS_ID_LIST_ATTRID
SERVICE_RECORD_STATE_ATTRID
SERVICE_ID_ATTRID
PROTOCOL_DESCRIPTOR_LIST_ATTRID
BROWSE_GROUP_LIST_ATTRID
LANGUAGE_BASE_ATTRID_LIST_ATTRID
SERVICE_INFO_TIME_TO_LIVE_ATTRID
SERVICE_AVAILABILITY_ATTRID
BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRID
DOCUMENTATION_URL_ATTRID
CLIENT_EXECUTABLE_URL_ATTRID
ICON_URL_ATTRID
SERVICE_NAME_ATTRID
SERVICE_DESCRIPTION_ATTRID
PROVIDER_NAME_ATTRID
SDP_UUID
UDP_UUID
RFCOMM_UUID
TCP_UUID
TCS_BIN_UUID
TCS_AT_UUID
OBEX_UUID
IP_UUID
FTP_UUID
HTTP_UUID
WSP_UUID
BNEP_UUID
UPNP_UUID
HIDP_UUID
HCRP_CTRL_UUID
HCRP_DATA_UUID
HCRP_NOTE_UUID
AVCTP_UUID
AVDTP_UUID
CMTP_UUID
UDI_UUID
L2CAP_UUID
IAC_GIAC
IAC_LIAC
BluetoothError
is_valid_address
is_valid_uuid
to_full_uuid
sdp_parse_size_desc
sdp_parse_uuid
sdp_parse_int
sdp_parse_data_elementSequence
sdp_parse_data_element
sdp_parse_raw_record
sdp_make_data_element
__version__
_dbg
have_widcomm
dll
sysroot
_msbt
msbt
bt
discover_devices
read_local_bdaddr
lookup_name
BluetoothSocket
advertise_service
stop_advertising
find_service
DeviceDiscoverer
[Finished in 0.417s]

Additionally, from inquiry.py:

import bluetooth

print("performing inquiry...")

nearby_devices = bluetooth.discover_devices(
    duration=8, lookup_names=True, flush_cache=True, lookup_class=False)

print("found %d device(s)" % len(nearby_devices))

for addr, name in nearby_devices:
    try:
        print("  %s - %s" % (addr, name))
    except UnicodeEncodeError:
        print("  %s - %s" % (addr, name.encode('utf-8', 'replace')))

...and again cntrl-shift-b to the results in the Script output window at the bottom of my Atom screen:

performing inquiry...
found 1 device(s)
98:FD:B4:47:0D:14 - HP Bluetooth Mouse X4000b
[Finished in 10.601s]

Seems that my access to my Python modules is working now.



来源:https://stackoverflow.com/questions/53455945/adding-modules-in-atom-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!