I am using terminal to try to run my code and I got it to run yesterday, but the username was tests-MacBook-Pro-2:sunkunoil maryjacketti$ and today it is tests-MacBook-Pro-2:~ maryjacketti$ and it won't run the code. The code is inside the folder sunkunoil on my computer. How do I get the terminal to say prompt the command like yesterday?
You have to change the directory that you are working in. This isn't an issue with the user that you are using, but rather where you are in the file system.
cd /desktop/sosim/sunkunoil
python [what ever your program name is]
The structure of terminal is:
[machine]: [current folder] [user]$
You are currently in home which is represented by ~.
On a Mac, the paths are at different locations. For posterity, you can either change to a relative path
cd SOSim/sunkunoil
or an absolute path
cd /Users/maryjacketti/SOSim/sunkunoil
The first part is where you start out when you start the Terminal app. It is also called you HOME directory.
Related
So I was following the tutorial on some basic set-up (cloning the folder onto my own pc) of a project
If you have chosen to install python (and already set up PATH variable
for python.exe) then follow the instructions:
Open the command prompt and change the directory to the project folder
as mentioned above by running the below command
cd C:/your cloned project folder path goes here/
then run the below command
python.exe C:/your cloned project folder path goes here/
And then I got:
C:\Users\user_name\project_name>python.exe C:\Users\user_name\project_name
C:\Users\user_name\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_abc1d2efgh3i4\python.exe: can't find '__main__' module in 'C:\\Users\\user_name\\project_name'
when I entered the second command. (note: I cloned the project folder to \user_name\project_name).
Yet I can't find much helpful information for this problem as it usually appears within python but not the command prompt of the computer itself. I have checked that I did set up the PATH variable for python.exe. And the path I entered for the 2 commands is the same. What may be wrong and how do I fix it?
Here is the cap screen of the content I was running, if helpful. The parts covered by blue are just user_name and project_name and some irrelevant information.
click to see cap screen (I am not allowed to embed pictures in posts as I haven't earned enough reputation.)
You see this error because C:\Users\user_name\project_name is a folder and python doesn't know how to handle it. Instead, you need to specify the name of a python script. These are files that usually end with a .py extension.
I am going through the Python Crash Course book 2nd Edition and trying to set up Sublime Text 3 programs to run from a terminal. I have made sure Sublime Text is running Python and have saved a simple Print("Hello) program to a file called python_work. Now I try and run the program from Command Prompt,
I enter cd Desktop\python_work and I get the answer The system cannot find the path specified.
The command prompt should show \Desktop\python_work> and I would contiune from there.
Why do I get this message and how can I fix it? (I am using Windows 10)
I was getting The system cannot find the path specified because the location of the python_work folder was not directly located on my desktop, the location was C:\User\Me\OneDrive\Desktop. I either had to move the file directly to my desktop or use the command cd OneDrive\Desktop\python_work. So if you get The system cannot find the path specified go to properties on your folder and check the exact location and try inputting that into command prompt.
So I dont know what happened but my python files are not being executed by the command prompt.
C:/Users/MOHAMMED/Local/Programs/Python/Python39>button.py
Above is what im trying to do. but it keeps giving me errors.
This file does not have an app associated with it for performing this action.
I have been using cmd prompt everyday to execute python files but today it is not working. any help is appreciated.
(I use IDLE to do my code btw)
edit:
So this is what i do when im trying to execute files using cmd prompt.
I go into the directory of where all the python files are stored by using cd command.
2)then i just say the name of the file and it works.
but today i installed pip by using setup program and after pip was installed, i cant execute python file using cmd prompt.
You are trying to execute the wrong way it should be:
python button.py
If that doesn't work then :
The complete path of python.exe can be added by:
Right-clicking This PC and going to Properties.
Clicking on the Advanced system settings in the menu on the left.
Clicking on the Environment Variables button on the bottom right.
In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable.
Clicking on New and entering Python’s install directory.
Then again try :
python button.py
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 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 )