简体   繁体   English

未能添加新目录路径以在终端中运行 python 脚本

[英]Failure to add new directory path to run python script in terminal

I know this has been discussed a bunch but nothing I have read online has worked.我知道这已经讨论了很多,但我在网上阅读的内容都没有奏效。 Note: I am a novice at terminal.注意:我是终端的新手。 Running Mac OS 10.14.5 Mojave运行 Mac OS 10.14.5 Mojave

I am able to run Python scripts in terminal if I first enter:如果我第一次输入,我可以在终端中运行 Python 脚本:

cd PythonScripts

(which is the folder they live in: /Users/myname/PythonScripts ) (这是他们所在的文件夹: /Users/myname/PythonScripts

My goal is to be able to run ./myscript.py from terminal without having to tell it where to look each time, so I want to permanently add the PythonScripts folder to the directory path.我的目标是能够从终端运行./myscript.py ,而不必每次都告诉它在哪里查看,所以我想将 PythonScripts 文件夹永久添加到目录路径中。

I have tried sudo nano /etc/paths and added /Users/myname/PythonScripts I save and restart terminal and check it with echo $PATH and see it's been added.我尝试了sudo nano /etc/paths并添加了/Users/myname/PythonScripts我保存并重新启动终端并使用echo $PATH检查它并查看它已被添加。 I then try ./myscript.py and get the message:然后我尝试./myscript.py并收到消息:

-bash: ./myscript.py: No such file or directory -bash: ./myscript.py: 没有这样的文件或目录

What am I doing wrong?我究竟做错了什么? Again, I don't know the lingo, just following what I read online.同样,我不知道行话,只是按照我在网上阅读的内容。

1) You need in your python script a header like this (set this to your python location) 1)您需要在 python 脚本中像这样的 header (将其设置为您的 python 位置)

#!/usr/local/bin/python3

2) Add your folder to your Path on your.zshrc or.bash_profile 2)将您的文件夹添加到 your.zshrc 或 .bash_profile 上的路径

export PATH=/Users/myname/PythonScripts:$PATH

3) Set execution permissions for every script you want to run 3)为您要运行的每个脚本设置执行权限

chmod + x myscript.py

Open a new terminal and you should be able to call myscript.py from anywhere打开一个新终端,您应该可以从任何地方调用 myscript.py

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

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