简体   繁体   English

从命令行运行 python 脚本时,import 语句不起作用

[英]import statement is not working when running python script from the command line

I need to run a python script from the command line (OS = Debian wheezy, python -version 3.5).我需要从命令行运行 python 脚本(OS = Debian wheezy,python -version 3.5)。

I used PyCharm (community edition) to write the script and it is working from inside the IDE.我使用 PyCharm(社区版)编写脚本,它在 IDE 内部运行。

I used sys.path.append command to add the directory containing the package I want, then followed it with this import line:我使用sys.path.append命令添加包含我想要的包的目录,然后使用以下导入行:

from package_name,file_name import ClassName

The Error message in the command line: ImportError: No module named 'package_name'命令行中的错误消息: ImportError: No module named 'package_name'

在此处输入图片说明

if you are running any xxx.py file and you face the import error though same script if run by any IDE works fine,its path issue.如果您正在运行任何 xxx.py 文件,并且在任何 IDE 运行正常的情况下通过相同的脚本遇到导入错误,则其路径问题。

What worked fine for me is: Go to file which shows import module issue and before importing module(for which issue is seen),add the path of module to sys using append.对我有用的是:转到显示导入模块问题的文件,在导入模块之前(看到哪个问题),使用 append 将模块的路径添加到 sys。

for example ,I was running the script file from conf path and my script was importing module situated in \\scripts\\Setup\\ so appended the path of module like below.例如,我正在从 conf 路径运行脚本文件,而我的脚本正在导入位于 \\scripts\\Setup\\ 中的模块,因此添加了如下所示的模块路径。

import sys
import os
conf_path = os.getcwd()
sys.path.append(conf_path)
sys.path.append(conf_path + '\scripts\Setup') 

then use import statement of module for which issue was thrown.然后使用引发问题的模块的导入语句。

I found the answer for my question above, and the problem was much easier than I thought.我在上面找到了我的问题的答案,问题比我想象的要容易得多。

Addressing the Problem解决问题

  • having many python packages in different directories在不同的目录中有许多 python 包
  • your script needs some/all packages, that are not in the standard lib-directory for your python installation (eg: prefix/lib/pythonVersion ).您的脚本需要一些/所有软件包,这些软件包不在您的 Python 安装的标准 lib 目录中(例如: prefix/lib/pythonVersion )。

Solution解决方案

Short term solution短期解决方案

As long you are using an IDE (eg PyCharm), it is sufficient within the code to add:只要您使用的是 IDE(例如 PyCharm),在代码中添加以下内容就足够了:

import sys sys.path.append("path/to/package")

As soon as you have to run your script from the command line, you will get an ImportError as mentioned in the Question above.一旦您必须从命令行运行脚本,您将收到上面问题中提到的ImportError

Better solution更好的解决方案

Add the directories of your packages and of your python installation to your shell-profile(eg: .bashrc ) using the command:使用以下命令将您的软件包和 python 安装目录添加到您的 shell-profile(例如: .bashrc ):

export PYTHONPATH=prefix/lib/pythonVersion:/path/to/packages

To get more info about PYTHONPATH , check this link要获取有关PYTHONPATH更多信息,请查看此 链接

In this case you will not need to append the path of your packages within your code :)在这种情况下,您不需要在代码中附加包的路径:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM