python filename.py in command line does not work - python

temp.py is a simply python scipt prints "hello". This is a simple example. None of my other scripts are running.
For example, something like python code.py arg1 arg2 does not work.
What are the possible reasons and solutions?
EDIT: After following advice from comments, I think it works now.

type "python" in cmd and enter. If you don't enter into the python command line then you don't set the environment variables and your system does not see the python path.

like the comment suggested, check the ENV_VARIABLES, see where your python module is installed and what version you have. If all those things look good,
then, check your code, a lot of times you maybe missing the
#!/usr/bin/env python3
at the top of the file, because the cmd line interpreter may not know if this is a python script. So include that shebang executable at top, if that doesn't work either. Pls upload your code on here for others to review...

Related

Is there a way to execute .py scripts from path in windows?

I would like to use a python script anywhere within command prompt. This is possible in unix, but I couldn't find anything for windows. Do I have to convert it to .exe?
Edit: not sure why this is being downvoted, maybe it's a silly question but I can't find any similar threads here and I can't be the first person to want to execute .py scripts from their path...
Edit 2: I think my wording was unclear. I would like to know a method to execute python scripts in Windows without needing to specify python path/to/script.py every time. Here is a solution on Linux where the shebang statement invokes the python interpreter, and the script in question can be easily placed in bin: How do I install a script to run anywhere from the command line? . Does there exist a solution like this for Windows?
Here's a solution for running myScript.py:
add to the myScript.py file a first line #!python (or #!python3 if you want to use Python 3)
For instance:
#!python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
change the "opens with" property of myScript.py to py.exe (to find where it is use where py-- I have it in C:\Windows\py.exe)
put the script myScript.py somewhere in your Windows path
and now you should be able to type myScript.py anywhere in a command prompt and run the Python script with your chosen Python version.
See: https://docs.python.org/3.6/using/windows.html#from-the-command-line

Unable to source .profile in shell script

To start off, I am a complete noob to Debian, Python, and shell scripts, so please speak to me like I am a toddler.
I have a python script I am running through a virtualenv, and I want to execute it via a shell script. Here is what I'm typing in to the terminal to do so:
source .profile
workon cv
cd Desktop/Camera
python main.py
I tried turning this into a shell script, but I receive the error -- source: not found
I've found no answer to my problem, at least not in any terms I can understand. Any advice would be appreciated. Furthermore, before you answer, I also have no idea why it is I need to execute source .profile, I'm simply following a beginner guide for the project which can be found here: https://www.hackster.io/hackerhouse/smart-security-camera-90d7bd
Thanks in advance, and sorry if this is a dumb question.
Best practice for Shell would be shebang at the begging like #Veda suggested.
Execute the shell script using bash like bash shell.sh as the link suggests using relative locations rather than absolute ones
Add a hashbang at the top of your script (should be the first line):
#!/bin/bash
This will ensure you are running your shell-script in bash. Having a shell does not mean it's bash. Not all shells have the source function that you are using (bash has it).
Some prefer the following:
#!/usr/bin/env bash
Since you are a "beginner" I think it does not really matter. The first makes sure it's using the bash in /bin, the second is using the PATH variable to find bash, so the user of your script can provide it's own bash if he/she wants.

How to run Python script with one icon click?

Sorry, for the vague question, don't know actually how to ask this nor the rightful terminologies for it.
How to run a python script/bytecode/.pyc (any compiled python code) without going through the terminal. Basically on Nautilus: "on double click of the python script, it'll run" or "on select then [Enter], it'll run!". That's my goal at least.
When i check the "Allow executing of file as a program" then press [enter] on the file. It gives me this message:
Could not display "/home/ghelo/Music/arrange.pyc".
There is no application installed for Python bytecode files.
Do you want to search for an application to open this file?
Using Ubuntu 12.04, by the way and has to be python 2, one of the packages doesn't work on python 3. If there's a difference between how to do it on the two version, include it, if it's not to much t ask, thank you.
I know it doesn't matter, but it's a script auto renaming & arranging my music files. Guide me accordingly, stupid idiot here. :)
You should make the .py files executable and click on them. The .pyc files cannot be run directly.
Adding " #!/usr/bin/env python " at the top of the .py file works! Hmm, although don't appreciate the pop-up, but nevermind. :P
From PHPUG:
You do not invoke the pyc file. It's the .py file that's invoked. Python is an interpreted language.
A simpler way to make a python exectuable (explained):
1) Add #!/usr/bin/env python at the top of your python executable file (eg. main.py) (it uses the default python - eg. if using arch, that's py3 instead of py2. You can explicitly tell it to run python2/python3 by replacing python with it's version: ex. python2.7)
2) Write the code. If the script is directly invoked, __name__ variable becomes equal to the string '__main__' thus the idiom: if __name__ == '__main__':. You can add all the logic that relates to your script being directly invoked in this if-block. This keeps your executable importable.
3) Make it executable 'chmod +x main.py'
4) Call the script: ./main.py args args
install launcher software in ubuntu 12.04
step 1. paste this command in terminal without quotes
"sudo apt-get install --no-install-recommends gnome-panel"
Step 2. now launch it by ..
gnome-desktop-item-edit --create-new ~/Desktop
Step : in command textbox write
python path_of_your_pyc_file/filename.pyc
eg python /opt/test.pyc
and haha!! you have done.. congrats :)
please check link how to install launcher here
https://askubuntu.com/questions/64222/how-can-i-create-launchers-on-my-desktop

usr/bin/env: bad interpreter Permission Denied --> how to change the fstab

I'm using cygwin on windows 7 to run a bash script that activates a python script, and I am getting the following error:
myscript.script: /cydrive/c/users/mydrive/folder/myscript.py: usr/bin/env: bad interpreter: Permission Denied.
I'm a total newbie to programming, so I've looked around a bit, and I think this means Python is mounted on a different directory that I don't have access to. However, based on what I found, I have tried to following things:
Change something (from user to exec) in the fstab: however, my fstab file is all commented out and only mentions what the defaults are. I don't know how I can change the defaults. The fstab.d folder is empty.
change the #! usr/bin/env python line in the script to the actual location of Python: did not work, same error
add a PYTHONPATH to the environment variables of windows: same error.
I would really appreciate it if someone could help me out with a suggestion!
In my case the problem was in the missing executable flag on the file.
The solution for me was in the following code
chmod +x ./executed_file
You script should start with:
#! /usr/bin/env whateverelse ...
^ this first one is important
This seems to be a late answer, but may be useful for others. I got the same kinda error, when I was trying to run a shell script which used python. Please check \usr\bin for existence of python. If not found, install that to solve the issue. I come to such a conclusion, as the error shows "bad interpreter".
I would recommend you to 'run cygwin as administrator' Thanks.
EDIT:- try chmod for permissions read for more here
You should write your command as 'python ./example.py ',then fix it in your script.
This was in Git Bash for me. I changed the first line (shebang line) of the .py file being run, leaving the original, adding the one above it, and that worked:
#!python
#!C:\Users\Emiri\Anaconda\envs\_build\python.exe
("Emiri" doesn't exist on my system, was hardcoded in a zipline install)
In my case I used cmder and started bash as Admin instead of WSL
You can disable SELinux by using:
setEnforce 0
More info about how to use SELinux

What does the symbol "#!" mean in Python?

What does this line of code mean? Without it, my python3 http server can't understand and let the browser download an empty .py file (depend on the link to the .py file)
#! /usr/local/bin/python3
It's not a Python thing, it a hashbang (or shebang) line which indicates which interpreter should process the file.
The rules vary but, in its simplest form, a file with the name xyz (containing that as the first line), when run from the command line with xyz, will run it using that interpreter, similar to:
/usr/local/bin/python3 xyz
This is not a python specific notion, see http://en.wikipedia.org/wiki/Shebang_(Unix)
It's the shebang/hashbang line and a Linux/UNIX thing, not Python-related at all.
When executing the file, the kernel will see the #! magic and use whatever comes after it to execute the script. The actual program that gets launched by the kernel will be program-from-shebang script-file-path [script-args]
Note that it's usually not a good thing to include a .../local/... path but rather use e.g. #!/usr/bin/env python3 which will result in python3 being looked up in the current PATH which is much more portable.
That is not python-specific but is called Shebang and tells the operating system with which program to run this script.
UNIX Shebang? See http://en.wikipedia.org/wiki/Shebang_(Unix). The space between ! and the first / probably shouldn't be there.

Categories

Resources