I've found a few other people asking this question, but the answers for their problem are not helping me. I trying to learn python and trying to make a file executable.
The practice script runs when I type python ./userPrompt.py.
But when I try to run it as an executable (just ./userPrompt.py).
I've successful change it chmod +x
And here's what i have in the top line of the file:
#! /user/bin/env python
and here's the error I get
bash: ./userPrompt.py: /user/bin/env: bad interpreter: No such file or directory
But the thing is if I go to /user/bin I can find env. So it definitely exists. Why can't Ubuntu find it??
I appreciate any help people can give me on this.
The path is /usr, not /user.
Related
I´m trying to execute locally installed programs from a Python script (OSX), but they are not found, since /usr/local/bin is not in the PATH. Running os.environ gives only /usr/bin:/bin:/usr/sbin:/sbin.
It is probably a common/simple problem, but I´ve exhausted Google, and start feeling a little stupid :-)
How do you try to execute your programs (please provide us a minimal code sample)?
If you are using the subprocess package, you can try to provide the full path of your executable:
subprocess.run(["/usr/local/bin/my_program"], ...)
Else, you can try to append /usr/local/bin to the os.environ list.
When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)
I am looking to run a file I created in python from a matlab script. I have checked that my python file works if I run it from the python interface. However I have not been able to get my python to run from matlab. The following is the code situation I am in.
In matlab., I have the following code:(My file name is pgcode.py)
! python pgcode.py
and interchangeably I have use this code as well:
system('python pgcode.py')
The error that results in matlab is:
"python: can't open file 'pgcode.py': [Errno 2] No such file or directory"
I have set my PATH directory and I really think this is an issue with setting the path so that I can find the file I have created but I haven't been able to figure out how to do this. I am using windows and Python 2.7.5. Any help is much appreciated. Thanks in advance!
There might be another way to do this, but here are two options.
First replace system('python pgcode.py') with system('pgcode.py'). Make sure that pgcode.py has execute permissions and in on your PATH. If you're using a unix/linux/mac type system, make sure pgcode.py has #!/usr/bin/env python as the first line, that's called a shebang.
Option two, is to use the full path when you call system(pathon /full/path/to/pgcode.py).
Hope that helps.
Your $PATH should control where python comes from, but I don't believe it will control where your pgcode.py comes from - at least, not in the way you're using it now.
You might want to either use a #!/usr/bin/env python and make your script executable, or be very mindful of what directory you're in when you try to python pgcode.py (you can prepend "pwd;" to your python command to see), or specify a full path to pgcode.py.
HTH
I am new to python and I have just installed python 2.7.3 on Windows. I will also install django so I need to execute a file named ez_setup.py. I know it seems like an easy question and answer can be found in internet, but this is not the case. I tried lots of things and tried what internet says, the problem is still there and I cant find the problem!
I follow all the steps that is explained in tutorials in order for Python to work properly. (Installing steps and editing environment variables..)
Python's location is:
C:\Users\name\27
Command prompt starts like:
C:\Users\name>
I have put ez_setup.py file under both C:\Users\name\27 and C:\Users\name.
When I type "ez_setup.py" or "python ez_setup.py" or "\27 python ez_setup.py" and lot of combination, it says:
python: cant open file 'ez_setup.py': [Errno 2] No such file or directory.
What should I do? Where do I make mistake?
Just do:
python ez_setup.py
You need to say your machine that it should execute this .py file with python.
I'm writing a pretty basic application in python (it's only one file at the moment). My question is how do I get it so the python script is able to be run in /usr/bin without the .py extension?
For example, instead of running
python htswap.py args
from the directory where it currently is, I want to be able to cd to any directory and do
htswap args
Thanks in advance!
Simply strip off the .py extension by renaming the file. Then, you have to put the following line at the top of your file:
#!/usr/bin/env python
env is a little program that sets up the environment so that the right python interpreter is executed.
You also have to make your file executable, with the command
chmod a+x htswap
And dump it into /usr/local/bin. This is cleaner than /usr/bin, because the contents of that directory are usually managed by the operating system.
The first line of the file should be
#!/usr/bin/env python
You should remove the .py extension, and make the file executable, using
chmod ugo+x htswap
EDIT: Thomas points out correctly that such scripts should be placed in /usr/local/bin rather than in /usr/bin. Please upvote his answer (at the expense of mine, perhaps. Seven upvotes (as we speak) for this kind of stuff is ridiculous)
Shebang?
#!/usr/bin/env python
Put that at the beginning of your file and you're set
add #!/usr/bin/env python to the very top of htswap.py and rename htswap.py to htswap then do a command: chmod +x htswap to make htswap executable.
I see in the official Python tutorials, http://docs.python.org/tutorial/index.html, that
#! /usr/bin/env python
is used just as the answers above suggest. Note that you can also use the following
#!/usr/bin/python
This is the style you'll see for in shell scripts, like bash scripts. For example
#!/bin/bash
Seeing that the official tuts go with the first option that is probably your best bet. Consistency in code is something to strive for!