Currently, in order to launch all my python scripts from the terminal I have to go to the relative folder and use
$ python myscript.py
to launch the script. How can I make it so that I can just type
$ myscript
to launch that script, no matter what folder I'm currently in?
-
3Depends on your operating system. Please tag your question accordingly.martineau– martineau2019年07月25日 15:38:32 +00:00Commented Jul 25, 2019 at 15:38
-
If you're a Linux user check the last part there: openbookproject.net/thinkcs/python/english3e/…Flo– Flo2019年07月25日 15:38:54 +00:00Commented Jul 25, 2019 at 15:38
2 Answers 2
Assuming you are using linux/macos
- add this shebang at the beginning of the script
#!/usr/bin/env python3 - make sure you can execute the file by calling
chmod +x myscript.py
Now if you are in the same directory of the script you can run it simply with the command ./myscript.py.
To be able to run the script, no matter what directory you are in:
- move the script into a directory listed inside the
$PATHenvironment variable, like/usr/local/bin, or make a directory ad-hoc for your scripts and add that dir to thePATHvariable
You can now call your script by just typing its name in the terminal.
1 Comment
echo $PATH you can see what dir's are in the path and move the script thereYou can use alias in your bash_profile for mac or bashrc in linux.
.bash_profile
alias myscript="python /<dir_where_file_is_located>/myscript.py"
once alias is added then reload it
source ~/.bash_profile
Now you can by just type myscript and enter.
$ myscript
Comments
Explore related questions
See similar questions with these tags.