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 )
Related
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.
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 want my RPi to open run a python webserver and open Chromium to access it.
It should do it automaticaly on startup.
BUT
When I run the script via command
$ sudo python3 /home/sps-training/python/webserver.py
and open localhost on chromium, it says that it can't access HTML file in another folder (/deploy)
but when I open the dictionary first
$ cd /home/sps-training/python/
and then open the script
$ python3 webserver.py
it suddenly works!
So there are 2 possible solutions
The first one is to make it work by using:
$ sudo python3 /home/sps-training/python/webserver.py
The second one is to automatically access directory and the start the script
Right now I'm using /etc/profile to run it on startup (I just wrote at the last line with & on the end of the line)
Thanks a bunch for every advice!
btw sorry for grammar
Like Barmar said. It sounds like the issue you're running into is a working directory vs. current directory problem. When you run sudo python3 /home/sps-training/python/webserver.py you are launching webserver.py in the directory / and if it looks for "deploy/index.html" it's going to look in /deploy/index.html. However, when you cd into /home/sps-training/python/ and then run python3 webserver.py the working directory is now /home/sps-training/python/ and it will look for deploy/index.html in /home/sps-training/python/deploy/index.html
The best solution is to edit the python script to use absolute file paths.
Barring that, you need to cd to the correct working directory every time.
I have a python project with multiple files and a cmd.py which uses argparse to parse the arguments, in the other files there are critical functions. What I want to do is: I want to make it so that if in the command line I were to put cmd -p hello.txt it runs that python file.
I was thinking that I could just simply move the cmd.py file to somewhere like /usr/bin/ or some other directory included in the $PATH, however since I have other files which work with my cmd.py, there will be multiple files in my /usr/bin.
Another thing that I could do is to make a symbolic link between the cmd.py and /usr/bin/cmd like this: ln -s /path/to/cmd.py /usr/bin/cmd, but then where do i put the cmd.py? and is this best practice?
Note: I intend for this to work on Linux and MacOS X, not windows
The usual way to do this is to define a set of entry points in setup.py and let the packaging infrastructure do the heavy lifting for you.
setup(
# ...
entry_points = {
'console_scripts': ['cmd = cmd:main'],
}
)
This requires setuptools.
Here is some documentation for this facility: https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html
For one thing I don't recommend installation in /usr/bin as that's where system programs go. /usr/local/bin or another custom directory added to $PATH could be appropriate.
As for getting it to run like a typical program, name it cmd, wherever you put it, as the extension is not necessary, and add this line to the top of the program:
#!/usr/bin/env python
(You may want to specify python3 instead of just python if you want to ensure Python 3.x is used.)
Then it can be made executable with chmod +x <path to your program>. Ensure that you have the necessary privileges to do this (i.e. sudo may be necessary).
You can add a folder to your path.
in .bashrc add following
export PATH=[New_Folder_Path]:$PATH
put the python program in your path_folder created at step 1.
make it executable : chmod u+x [filename]
open a new terminal, and you should be able to call the python program
NOTE: make sure to put the shebang in your python-file : #!/usr/bin/env python3
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.