执行python程序 [英] Executing python program
问题描述
我一直在寻找答案的网站现在有一段时间了,但是这让我真的很头疼:
我使用Ubuntu 12.04,我想执行从终端的Python脚本,而不使用完整路径。
所以我加的/ home / kyril /蟒蛇/脚本/通过把以下内容./bashrc到PATH变量:
已经出口回声路径其他PATH = $ PATH:/家庭/ kyril / Python的/脚本/
PATH = $ PATH:/家庭/ kyril /猛砸/脚本/
导出路径科幻
(我知道if子句是没有必要的,但我不喜欢什么都有两次在我的道路,如果我键入EXEC的bash)。
现在的问题:这完全适用于我的bash脚本,所以让他们通过执行后搭配chmod我可以只输入 $ script.sh
,它是执行。但是如果我输入 $ python3 script.py
以下引发错误: python3:无法打开文件'script.py':[错误2 ]没有这样的文件或目录
如果我的完整路径脚本类型它的作品。任何人有一个想法,我做错了什么?我必须在目录添加到PYTHONPATH? (当我明白这不仅有利于进口模块)。
谢谢你们!
在调用 python3
直接,蟒蛇运行你告诉它的脚本文件,而无需使用 $ PATH
找到它。 PYTHONPATH
无关 - 这是用于搜索Python模块
我猜您遇到当您通过自身运行 script.py
获取调用错误间preTER问题。我不知道你的脚本的第一行是什么,但它应该是这样的:
#!的/ usr / bin中/ env的python3
或者,如果你需要更精细的控制:
#!的/ usr / bin中/ env的python3.2
和Python的脚本,2
#!的/ usr / bin中/ env的python2
或者
#!的/ usr / bin中/ env的python2.7
您应该检查你的系统中存在这些可执行文件试图使用它们之前。
I have been searching the web for an answer now for quite a while, but this is giving me really headache:
I am using Ubuntu 12.04 and I want to execute a Python script from the terminal without using the full path. So i added /home/kyril/python/scripts/ to the PATH variable through putting the following into ./bashrc:
kyrilpathvariable="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kyril/Python/scripts/:/home/kyril/Bash/scripts"
if [ "$kyrilpathvariable" = "$PATH" ]; then
echo PATH already exported
else
PATH=$PATH:/home/kyril/Python/scripts/
PATH=$PATH:/home/kyril/Bash/scripts/
export PATH
fi
(I know the if clause is not necessary but I did not like to have everything two times in my PATH if I type exec bash.)
Now the problem: this perfectly works for my Bash scripts, so after making them executable via chmod I can just type $ script.sh
and it is executed. However if I type $ python3 script.py
the following error is raised: python3: can't open file 'script.py': [Errno 2] No such file or directory
if I type in the full path to the script it works. Anybody has an idea what I am doing wrong? Do I have to add the directory to the PYTHONPATH? (As I understood this only helps for importing modules).
Thanks guys!
When invoking python3
directly, python runs the script file you told it to, without using $PATH
to find it. PYTHONPATH
is irrelevant--that's used for searching for Python modules.
I'm guessing you're having issues with the wrong interpreter getting invoked when you run script.py
by itself. I don't know what the first line of your script is, but it should be this:
#!/usr/bin/env python3
Or if you need even finer control:
#!/usr/bin/env python3.2
And for Python 2 scripts:
#!/usr/bin/env python2
Or:
#!/usr/bin/env python2.7
You should check that these executables exist on your system before trying to use them.
这篇关于执行python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!