且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在终端上找不到 Python 模块,但在 Python shell、Linux 上找不到

更新时间:2023-08-21 11:38:16

您的 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.