在终端上找不到 Python 模块,但在 Python shell、Linux 上找不到 [英] Python modules not found over terminal but on python shell, Linux

查看:48
本文介绍了在终端上找不到 Python 模块,但在 Python shell、Linux 上找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的笔记本电脑上安装了 ubuntu 并且我已经安装了 python,在安装 python2.7.5 之后我试图在终端上运行一个 python 脚本,但是它说没有找到模块,我开始下载所有的模块但它仍然说未找到模块.升级到 python2.7.9 后,它仍然说相同,所以我安装了正确导入模块的 python iddle shell.

I have installed ubuntu on my laptop and i have installed python, after installing python2.7.5 i was trying to run a python script on terminal, but it said module no found, i started to download all the modules but it still said module not found. After upgrading to python2.7.9 it still said same so i installed python iddle shell which is importing the modules correctly.

为什么会这样?为什么它在 python shell 上工作而不是在终端上工作.终端仅识别诸如 sys、os.. 和一些内置模块之类的模块,但不识别已安装的模块.我将不胜感激.(我刚开始用linux)

Why is it happening ? why is it working on the python shell but not on terminal. terminal is only recognizing modules like sys, os.. and some built-in modules but not the installed. I would appreciate the help. (I just started to use linux)

推荐答案

您的 Python shell 使用的 PYTHONPATH 似乎与您在终端中执行的 Python 不同.您可以通过键入

It seems that your Python shell uses a diffenrent PYTHONPATH than the python you execute in the terminal. You can verify that by typing

import sys
print sys.path

在两个 shell 中并比较两个输出.我假设在终端中启动的 python 的输出中缺少安装的模块路径.

in both shells and comparing the two outputs. I assume that the installed module path(s) are missing in the output of the python started in the terminal.

你可以通过在你的 shell 中定义一个 PYTHONPATH 来解决这个问题:

you can solve this by defining a PYTHONPATH in your shell:

export PYTHONPATH=...

... 表示 python shell 输出的所有路径,以 :

... means all paths of the python shell's output separated by :

不要使用空格.如果其中一条路径中有空格,请用引号将 ... 括起来

Don't use spaces. If there spaces in one of the paths, surround ... with quotes

export PYTHONPATH=带空格的路径:其他路径:路径"

从您输入导出命令的那个终端启动 python.尝试导入您的模块.如果可行,请将导出内容附加到您的主目录中的 .profile 中.

Start python from that terminal where you entered the export command. Try to import your modules. If it works, make the export permanent by appending it in your .profile located in your home directory.

ls -a $HOME 

显示文件(以及许多其他文件;-).它是一个 .file..files 隐藏在一个简单的 ls 上.

shows the file (and many others ;-). It is a .file. .files are hidden on a simple ls.

这篇关于在终端上找不到 Python 模块,但在 Python shell、Linux 上找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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