【问题标题】:How to import a library in puTTY如何在 puTTY 中导入库
【发布时间】:2015-12-19 04:28:54
【问题描述】:

我是 Linux 世界的新手,并且正在为 BBB(Beaglebone Black,我相信你们都知道)的项目而苦苦挣扎,我的学习曲线相当缓慢,但它正在变得更好。

由于是Debian,所以puTTY是我在Windows中的终端,但是现在我需要导入一个库,它无法理解import,因为它是一个Python命令,我必须使用shebang行。

但是如何在 puTTY 中使用它?使用#!/usr/bin/python 在终端中的命令显然不起作用之前?

当我输入:

import Adafruit_BBIO.GPIO as GPIO

它显然说:

-bash: import: command not found

我正在尝试这样做:
https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/using-the-bbio-library

而且我已经通过了这一步:
https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/installation-on-ubuntu

因为我的列表是这样的:

debian@beaglebone:~$ ls -al

total 44    
drwxr-xr-x 4 debian debian 4096 Dec 19 03:28 .    
drwxr-xr-x 3 root root 4096 Nov 12 21:12 ..    
drwx------ 3 root root 4096 Nov 12 21:14 .BBIOServer    
-rw-r--r-- 1 debian debian 220 Nov 12 2014 .bash_logout    
-rw-r--r-- 1 debian debian 3515 Nov 12 2014 .bashrc    
-rw------- 1 root root 7 Nov 12 21:18 .gitconfig    
-rw------- 1 110 116 113 Nov 12 21:15 .npmrc    
-rw------- 1 debian debian 186 Nov 12 21:12 .pastebinit.xml    
-rw-r--r-- 1 debian debian 675 Nov 12 2014 .profile    
-rw------- 1 debian debian 64 Nov 12 21:12 .xsessionrc    
drwx------ 2 debian debian 4096 Nov 12 21:12 bin

有什么想法吗?

【问题讨论】:

  • 只是让您知道您的链接已损坏
  • 你只能在python文件中使用#!/usr/bin/python,即shebang(或hashbang)。如果您在 Shell 中,我认为您需要在导入任何 python 模块之前通过python 命令输入 Python Shell
  • 谢谢@wilbur。它们现在已经修好了。
  • @KevinGuan 如果您检查第二个链接,我想我已经安装了 Python Shell。谢谢你完美的编辑顺便说一句。或者我最好问问,我该怎么做?
  • type python ... 然后当shell启动时输入from ... import ...

标签: python import putty beagleboneblack


【解决方案1】:

来自your second link,我想你错过了这部分:

您也可以通过执行python命令来启用解释器来验证,并运行以下代码(当您在终端中看到>>>时,您就可以知道您在正确的位置)

Bash Shell 不是 Python Shell。当然你安装了它,但你没有运行它。检查这个:

[kevin@Arch ~]$ import sys
bash: import: command not found

[kevin@Arch ~]$ python
Python 3.5.1 (default, Dec  7 2015, 12:58:09) 
[GCC 5.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> 

所以我认为你需要输入 python 命令进入 Python Shell,然后运行 ​​Python 命令(例如,import),正如我在 cmets 中所说的。


要了解更多关于什么是 Python Shell,[文档在此处]。

【讨论】:

  • 非常感谢!我必须停止如此不耐烦。现在它起作用了。虽然当我使用命令python 时它显示>>> 它仍然无法导入该库debian@beaglebone:~$ python Python 2.7.9 (default, Mar 1 2015, 13:48:22) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Adafruit_BBIO.GPIO as GPIO; print GPIO Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named Adafruit_BBIO.GPIO,但是当我使用sudo 时它工作得很好。我应该继续使用这种格式还是有解决方法?
  • @Mohsen:嗯...你是什么意思当我使用sudo 时它完美地工作
  • 我的意思是我需要以sudo python运行python,否则我无法使用import命令。
  • @Mohsen:哦,也许是因为只有 root 用户才能导入该模块。我不知道为什么,但很高兴听到您的问题解决了 :)
猜你喜欢
  • 2012-10-13
  • 2012-06-13
  • 2013-10-03
  • 2021-01-08
  • 2019-01-13
  • 2018-01-19
  • 2017-12-14
  • 2013-09-06
  • 2017-05-06
相关资源
最近更新 更多