Let's say I have a program called "hello". It is a python script and I want to run it at the linux command line.
I've been able to remove the .py extension as well as the interpreter.
Now when I run it it is [user..path] $ ./hello
it works, but how do I remove the "./". I've already applied the chmod a+x argument to the file and a shebang line at the front of my script.
Basically, I am asking how to run the script as $ hello
Add . to the end of your $PATH variable.
PATH=$PATH:.
Then it will search the current directory when looking for programs to run.
Note that you should not generally put . at the beginning of $PATH. If you cd into someone else's directory, and they have a program that's the same name as a system program (e.g. ls), you'll execute their version instead of the real one.
You could also create your own directory where you install programs, and put that directory in $PATH.
PATH=~/bin:$PATH
Now you copy hello to your ~/bin directory and run hello.
Related
Problem to solve
I have a python script called myscript.py in a directory.
To run myscript.py, I have to open terminal in the same directory and do python myscript.py or python path_to_the_py_file
What I want to achieve
I can open a instance of terminal anywhere, anytime and do myscript or myscript arguments -options. By doing this, I should be able to run the script.
I want to implement something like pip command or the howdoi package.
When I enter myscript in terminal I should not get:
myscript: The term 'myscript' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
If you have any confusion please ask in comments. if the title needs improvement, feel free to edit the question.
There seems to be quite a few things in this question.
First, you want to make you file executable.
chmod +x /path/to/your/script.py
Then, since this is an interpreted language, you’ll have to specify the interpreter in a shebang at the top of the file.
#!/usr/bin/env python
OR
#!/usr/bin/env python3
Finally, you’ll have to put it in your PATH environment variable, which can be done in a variety of ways, simplest might be symlink in ~/bin or ~/.local/bin, depending on what you have.
Also, this will make script.py available as an executable, you can rename it or rename its symlink to just script if you don’t want .py.
I have been looking into bash and shell recently and been trying to work out how git is able to make a terminal command that runs C scripts in the current directory e.g git init, git push/pull etc.
I have been trying to simulate it in python, by making an executable python script in my home directory,
#!/usr/bin/env python
import os
print("current_dir: ",os.getcwd()) #prints /Users/usr/folder/script.py
Then I create a .command file that calls the python script
cd
cd FOLDER/
python3 SCRIPT.py
and editing the bash profile to export a variable to run the .command file.
export mycommand=/Users/urn/folder/command.command
Although this is not even nearly close to the way git achieves its command line. For example, when I run my script is not actually a terminal command it is just an environment variable, hence the $.
$mycommand
Secondly, this goes to the directory of the python file and runs the script from with that directory, therefore the output will always be the same
/Users/usr/folder/script.py
Although in git it runs the file in the current directory. Therefore the print statement would change depending on the terminal directory.
How am I able to create my own 'terminal command' such as git init to run my python script in whatever directory I'm in. Ps, I'm on mac.
Any help would be greatly appreciated :)
It sounds like you are missing at least two basic concepts:
The search path: when you issue a command that is not a shell function or built-in and that has an unqualified name (one with no / characters), the shell searches a list of zero or more directories for a matching executable file. This list of directories is the "path", and it is stored in the PATH environment variable. You can change it if you wish. This is how the shell finds the git program when you do not specify a path to it.
Executable scripts: Python, shell, Perl, etc. programs that must be run via an interpreter can be made executable by name alone by including an appropriate shebang line as the very first line and assigning an executable mode. You include an appropriate shebang line in your example Python program, in fact, but you seem not to understand its significance, because you explicitly launch the script via the python3 command. The shebang line is just another comment to Python, but it is meaningful to the system.
It seems like you probably also are missing some other concepts, like the fact that your script doesn't need to be in the current working directory for you to run it via the python3 launcher, path notwithstanding. Just specify its full pathname. Alternatively, Python has its own variation on a path, PYTHONPATH, by which it can locate modules and packages. These are alternatives, however -- the first two points are enough to achieve your apparent objective. Specifically,
Keep the shebang line in your script, though your default python is probably v2.7, so if you really want to run it specifically via python3 then modify the shebang line to say so:
#!/usr/bin/env python3
Make sure the file has executable mode. For example,
chmod 0755 /Users/urn/bin/SCRIPT.py
Then you should be able to execute the script from anywhere via its full pathname.
To access it from anywhere via its simple name, ensure that the directory containing it is in your path. For this purpose, it would be wise to choose an appropriate directory, such as /usr/local/bin or /Users/urn/bin (you may need to create the directory first). Whichever you choose, ensure that that directory is in your PATH. For example, edit /Users/urn/.bash_profile, creating it if necessary, and ensure that it contains (say) the commands
PATH=$PATH:/Users/urn/bin
export PATH
That will take effect in new Terminal windows you open afterward, but not automatically in any that are already open. In those windows, you will be able to run the script, from anywhere, via its simple name.
On Windows 10, Python 3.6.1, after I added E:\learning python exercise in PYTHONPATH by the GUI or through .pth file. Ihen C checked the os.sys.path, it reads:
But when I want to run a file through the cmd prompt, it pops out error:
Python does not use PYTHONPATH for scripts - it uses them for modules. So you could try:
python -m script1
but that is not the way scripts should be run. You should supply the path name to python or assume it is in the current directory. For example:
python "E:\learning python exercise\script1.py"
The double quotes are required because you have elected to have spaces in your directory name - I suggest that you might want to stop doing that, spaces in path names just makes life more difficult.
Alternatively, change your current directory, either using the cd command or by setting the "start in" properties to your cmd.exe shortcut. For example:
cd "E:\learning python exercise"
python script1.py
You only have to do the cd once in each cmd.exe session.
If you have setup the filename association for python (should have been done at installation time), then you should only need:
cd "E:\learning python exercise"
script1.py
I have a script/app/program written in python 3. I uploaded it to my Ubuntu box and changed the permission to allow execution by all. I am able to run python myapp.py with no problem but I cannot run myapp.py. I get an error that it is not a recognized command. I have at the top
#!/usr/bin/env python3
That should be right from all that I've read so far. I even tried
#!/usr/bin/python3
in the program referred to as myapp.py
Neither of them work.
I was following an online course and all was going well until we got to that point of running python scripts like regular programs by setting the execute setting.
If you are talking about, executing it from any directory, you need to do two things.
Setting the path variable. Lets say I need to execute Test.py, which is in Desktop, from any directory
export PATH=$PATH:/home/thefourtheye/Desktop/
Giving execute permission to the file
chmod 755 /home/thefourtheye/Desktop/Test.py
Then I can execute it by simply typing Test.py.
You cannot execute file in unix by name without directory name due to some security considerations, so you have to add . as directory (it will look like ./myapp.py)
. isn't on your path (and it probably shouldn't be for security reasons), so Ubuntu won't look in the current directory for a myapp.py program to run. You need to explicitly specify ./myapp.py to indicate that you want to run the myapp.py file in the current directory.
You should either call it as
./myapp.py
or the current directory should be in the PATH environment variable (either as full directory path name or as '.' which indicates the dynamic current path), check with
echo $PATH
to add it you can run
export PATH=$PATH:.
If you want to run it like:
user#machine:~$ myapp.py
You must put the script in the /usr/bin/ or /bin/ or something similar. In other case you must run like:
user#machine:~/appFolder/$ ./myapp.py
did you try:
$ sudo chmod a+x myapp.py
then run the python code using:
$ ./myapp.py
For linux users, may be text format dos/unix problem!
try,
sudo dos2unix name_of_your_script
then shebangs will probably get to work properly! That was what happened to me.
PS:
Ofcourse, also your script has to be executable
(sudo chmod +x name_of_your_script )
I'm trying to run a Python script from the command line, but I'm getting the error
$ python pscan2.py
python: can't open file 'pscan2.py': [Errno 2] No such file or directory
However, I also have
$ which pscan2.py
/usr/bin/pscan2.py
and
$ echo $PATH
/usr/lib/:/usr/local/bin:/usr/bin:/cygdrive/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/MinGW64/bin:/cygdrive/c/Dwimperl/perl/bin:/cygdrive/c/Dwimperl/perl/site/bin:/cygdrive/c/Dwimperl/c/bin:/cygdrive/c/python27: C:/python25:/usr/lib/lapack:/usr/openwin/bin
I can import it within Python, since I've added the directory to the PYTHONPATH, and that works fine, but I have to specify the directory to get it to run at the command line, even though which can find it.
EDIT: emacs can't find it either...
I don't think you need to say python in the command line. If you chmod +x it and it is in your path, You should be able to just call it like
$ pscan2.py
and it should work. I don't know the specifics of cygwin but if you have the shebang line, it will automatically run it as a python script.