简体   繁体   English

从我自己的目录运行Python时,为什么Python无法运行该程序?

[英]Why does Python fail to run a program when I run it from its own directory?

This is not working: 这不起作用:

numbers is a directory: 数字是目录:

$ cat numbers.py
import networkx as nx


~/numbers $ python2.7 < numbers.py

This gives me a few errors which end with something like: 这给了我一些错误,这些错误的结尾是:

'module' object has no attribute 'Number'

This is working: 这正在工作:

~ $ python2.7 < numbers/numbers.py

I installed networkx following these instructions: http://networkx.github.io/documentation/latest/install.html 我按照以下说明安装了networkx: http ://networkx.github.io/documentation/latest/install.html

Download the source (tar.gz or zip file) from https://pypi.python.org/pypi/networkx/ or get the latest development version from https://github.com/networkx/networkx/
Unpack and change directory to the source directory (it should have the files README.txt and setup.py).
Run python setup.py install to build and install
(Optional) Run nosetests to execute the tests if you have nose installed.

The tests run fine, but I don't understand why the trivial program containing only "import networkx as nx" won't run. 测试运行正常,但是我不明白为什么仅包含“将networkx作为nx导入”的普通程序无法运行。

What is the difference between these two situations ? 这两种情况有什么区别?

You are passing in the script on STDIN, instead of on the command line, so Python adds the current working directory to sys.path instead of numbers . 您正在STDIN而不是在命令行上传递脚本,因此Python将当前的工作目录而不是numbers添加到sys.path中。

Normally, Python adds the directory of the script as first element in sys.path : 通常,Python将脚本目录添加为sys.path第一个元素:

As initialized upon program startup, the first item of this list, path[0] , is the directory containing the script that was used to invoke the Python interpreter. 如程序启动时所初始化,此列表的第一项path[0]是包含用于调用Python解释器的脚本的目录。 If the script directory is not available (eg if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. 如果脚本目录不可用(例如,如果交互式调用解释器或从标准输入中读取脚本),则path [0]为空字符串,该字符串将引导Python首先搜索当前目录中的模块。

If you are having import problems, then it sounds as if the wrong module is being imported; 如果您遇到导入问题,那么听起来好像导入了错误的模块; perhaps you are masking a stdlib module with one with the same name, locating in numbers . 也许您正在用一个具有相同名称的stdlib模块屏蔽它们,并在numbers定位。 Rename that module. 重命名该模块。

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

相关问题 当我从 SQL 服务器代理运行时,为什么我的 Python 脚本会失败? - Why does my Python script fail when I run it from SQL Server Agent? 为什么此python脚本仅在从命令行运行时才会失败? - Why does this python script only fail when run from the command line? 为什么我的Python程序在从Java调用时会失败? - Why does my Python program fail when called from Java? 为什么Python无法运行ant build? - Why does Python fail to run ant build? 当我从目录外部导入此函数时,为什么我的python import语句失败? - Why does my python import statement fail when I import this function from outside the directory? 有没有办法从其目录运行python文件 - Is there a way to run a python file from its directory 为什么python,当我运行我的程序时出现错误&#39;预期缩进块? - Why does python, when I run my program comes up with the error 'expected an indented block? 为什么我的python程序双击.sh文件不运行 - Why does my python program not run when I double click the .sh file 为什么我在运行PowerShell程序时,PowerShell在后台运行? - Why does PowerShell run in the background when I'm running a Python program? 为什么该程序从Aptana IDE运行而不是在python命令中运行? - Why does this Program run from Aptana IDE and not in python command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM