Python3没有这样的文件或目录 [英] Python3 no such file or directory
问题描述
我正在尝试制作python3可执行脚本并从shell运行它们.我在系统上安装了python 3.4.0.因此,我将'/home/spandan/python_codes'目录添加到 PYTHONPATH ,因为我打算将脚本和模块保存在这里.
I am trying to make python3 executable scripts and run them from shell.I have python 3.4.0 installed on my system. So, I added '/home/spandan/python_codes' directory to PYTHONPATH, as I am planning to keep my scripts and modules here.
但是,在尝试执行这些命令时,系统会抛出上述错误,除非我进入python_codes目录然后执行它们,否则脚本将不会执行.
However, while trying to execute these, the above error is thrown by the system, and the scripts wont execute unless I go into the python_codes directory and then execute them.
执行python程序:在这里我发现PYTHONPATH与编写脚本无关,以及如何设置python shebang.所以我将我的设置为#!/usr/bin/env python3.4.0
Executing python program : Here I found out that PYTHONPATH is irrelevant while making scripts, and also how to set the python shebang. So I set mine as #!/usr/bin/env python3.4.0
对吗?
推荐答案
您不必将python代码放在全局路径中.只需使您的python 3.4解释器解释器全局可用即可.为此,请在主目录中编辑.bash_profile或.bashrc文件,然后添加以下行:
You don't have to put your python codes in a global path. Just make your python 3.4 interpreter interpreter available globally. For that, edit .bash_profile or .bashrc file in your home directory and add the following line:
export PATH=${PATH}:/usr/bin/python3
这将使python3可执行文件,而不管您当前的工作目录如何.为了从您的codes目录执行代码,您只需要编写:
That will make python3 executable irrespective of your current working directory. In order to execute code from your codes directory, you just have to write:
$ python3 ./your_code.py
另一种方法是在代码的顶部添加shebang作为
Another way is to add the shebang at the top of your code as
#/usr/bin/python3
并修改权限为当前用户可执行(默认没有执行权限).
and change the permission to executable by the current user (by default it will not have execute permission).
$ chmod 744 your_code.py
然后直接执行脚本
$ your_code.py
希望我能解决您的问题.
I hope I could address your problem.
这篇关于Python3没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!