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.
我正在尝试创建python3可执行脚本并从shell中运行它们。我的系统上安装了python 3.4.0。因此,我将'/home/spandan/python_codes'目录添加到PYTHONPATH中,因为我打算在这里保留我的脚本和模块。
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_codes目录并执行它们。
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程序:在这里,我发现在编写脚本时,PYTHONPATH是不相关的,以及如何设置python shebang。所以我把我的设为#!/usr/bin/env python3.4.0
Is it Correct? Please help.
是正确的吗?请帮助。
Thanks, Spandan.
谢谢,Spandan。
1 个解决方案
#1
3
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:
您不必将python代码放在全局路径中。让您的python 3.4解释器在全球范围内可用。为此,在主目录中编辑.bash_profile或.bashrc文件,并添加以下行:
export PATH=${PATH}:/usr/bin/python3
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可以执行,而不考虑当前工作目录。为了从代码目录执行代码,您只需编写:
$ python3 ./your_code.py
python3美元。/ your_code.py
Another way is to add the shebang at the top of your code as
另一种方法是在代码的顶部添加shebang as
#/usr/bin/python3
# / 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
chmod 744美元your_code.py
and then executing the script directly as
然后直接执行脚本as
$ your_code.py
your_code.py美元
I hope I could address your problem.
我希望能解决你的问题。
#1
3
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:
您不必将python代码放在全局路径中。让您的python 3.4解释器在全球范围内可用。为此,在主目录中编辑.bash_profile或.bashrc文件,并添加以下行:
export PATH=${PATH}:/usr/bin/python3
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可以执行,而不考虑当前工作目录。为了从代码目录执行代码,您只需编写:
$ python3 ./your_code.py
python3美元。/ your_code.py
Another way is to add the shebang at the top of your code as
另一种方法是在代码的顶部添加shebang as
#/usr/bin/python3
# / 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
chmod 744美元your_code.py
and then executing the script directly as
然后直接执行脚本as
$ your_code.py
your_code.py美元
I hope I could address your problem.
我希望能解决你的问题。