简体   繁体   中英

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

I am able to run Python scripts in terminal if I first enter:

cd PythonScripts

(which is the folder they live in: /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.

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. I then try ./myscript.py and get the message:

-bash: ./myscript.py: No such file or directory

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)

#!/usr/local/bin/python3

2) Add your folder to your Path on your.zshrc or.bash_profile

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

3) Set execution permissions for every script you want to run

chmod + x myscript.py

Open a new terminal and you should be able to call myscript.py from anywhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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