How can I run this code from my terminal/prompt/shell - python

I am new to coding and would like to know how do I run a program on a terminal thing. I am following a video in which a person types out the name of the file he wants to run and it runs. What terminal/prompt/window do I need to type this in. I have a photo of the video tutorial and a photo of my desktop with all of my terminal applications. I keep getting errors, please help.
Tutorial video showing the commands
my terminal/prompt type applications

Step 1: Create a python file in any directory of your choice
Step 2: edit python file and add some code (whatever you want to run)
Step 3: Open CMD by either clicking on Start and typing "cmd" or pressing and holding the "Start/Windows" button on your keyboard and pressing "R" on your keyboard, this will open a run window on the bottom left that looks like this
In that "run" window type cmd. this will open a window that looks like this
Step 4: After opening CMD you will be able to type in it. type cd (Stands for change directory) and type or paste the directory of where the python file is located and press "Enter" Now you are in the directory of the python file see this for more detailed instructions on changing directories Change Directories
Step 5: Type in the name of the python file, in this case, its "Hello World.py" and press "Enter" this will run the python file in cmd and show the result of the file in cmd
Good luck with your coding venture!

Related

Console not outputting information in PyCharm

I have a problem through pyautogui I need to know the position of my cursor but when I enter the code and press run in the console nothing happens
enter image description here
I'm just starting to understand python
You aren't running the file. Note how it says "Python Console" in the tab header. That indicates that no file is imported and run by default.
Right-click on the background of the editor, and press "Run file in Python console". You'll notice that it will open a new tab with a header of the file name, and your code will execute.

How to make Windows execute my Python script files when I click them

I have made a Python program that uses output.to_csv('results.csv'). When I run it in Spyder it creates that CSV file. But when I double click on it nothing happens. Cmd appears and closes but nothing on the folder, no results.csv.
What am I missing? What more do I have to do?
Run the program from the command line itself instead of double-clicking the .py file.
I assume you are on Windows since you mention CMD. First, cd into the directory containing your program. Then, run python <program>.py or python3 <program>.py depending on your installation.
This time, you will see any output or error messages that appear in CMD without it immediately closing.
If the .csv file really exists, you should be able to go to your File Explorer and find the file at the top of the "Quick Access" section. Right-click the file and hover over "Open With >". Then select Notepad and a notepad will open up showing your results.
If you do not see the file, then try running your program on the command prompt (for Windows):
Press the windows key and type "cmd" in the search bar.
Choose "Command Prompt"
Go to the dir of your program using the cd command
Type python <program name>.py
If there are no errors, follow the steps in the first paragraph.
Ok i guess windows is not recommended at all for this type of tasks. I mean running something simple as create such file is like trying to kill the Lernaean Hydra.
What i did is i just runned it with anaconda prompt and it worked sweet! Thanks for help. Thanks to all!
PS: I'm seriously considering changing to Linux after this
For anyone having the same problem, but have anaconda installed. 1) Open Anaconda Prompt, 2) use cd (1 space) then adress of the folder which contains your py program (eg. cd C:\Users\Bernie\Desktop\tasos) and hit enter, 3) on the next line that appears type: python program_name.py, 4)Hit enter, 5)success!

Command Prompt does not execute .py file correctly?

I use Adobe products (such as Premiere Pro/Photoshop) a lot and wanted to have Discord Rich Presence implementation.
I found a very useful python script for this: https://github.com/smokes/adobe-rpc
If I manually execute it (opens command prompt) it works perfectly fine, it detects my Discord and Premiere Pro/Photoshop and displays it correctly in Discord.
However I wanted to run this script when I launch Premiere Pro/Photoshop so I used a batch file for this:
#echo off
start "Premiere Pro" "C:\Program Files\Adobe\Adobe Premiere Pro CC 2019\Adobe Premiere Pro.exe"
ping -n 20 localhost
python "E:\Software\Python\Adobe Discord Rich Presence\adobe-rpc-master\rpc.py"
It opens the script as it should but when it actually launches the script, the script can not find Discord or the Adobe software.
It just states "Run Adobe/Discord App".
I have tested the script by opening a fresh Command Prompt and dragging the script in: same result as stated above. It can not find Discord or the Adobe software.
This is very strange to me as I can perfectly run it if I open it manually but not if I run it in the Command Prompt?
Furthermore, Python does not give an actual error message, just the "error message" that is built inside the script.
When I launch it manually, it opens in the Command Prompt so I am very confused as to why this happens.
Can anyone explain this/help me?
I am very new to the Command Prompt and Python.
Warning! This is comment and maybe answer both!
First of all: do you have Python 3.4>= added in PATH? If not add.
Second: Do you followed instructions in README.md? If not follow!
Third: Which Adoble application do you use and is it available in adobe-rpc's repository? If not then wait for next update of adobe-rpc.
Fourth: Try to open script in it's directory and this is how:
Go to script's folder
Hold SHIFT and click RMB, also called Right Button Mouse. In menu Open Command Prompt here or Open Powershell here must be displayed.
If Open Command Prompt here is displayed instead of Open Powershell here, click it.
Now do: python scriptname.py ~ I don't know that script usage and if it requires arguments, just do python scriptname.py arguments.
If Open Powershell here is displayed instead of Open Command Prompt here. Go back, but first copy directory.
Then press WIN+R and in Run input type cmd.
In Command Prompt(CMD) type cd and paste your directory, then hit Enter.
Now do same as in 4. line.
Maybe this would help.
Otherwise I don't know.

Python py not executing in other PC

I have a technical questions on python script.
I have developed code on my laptop and I want to move it in another one.
I installed python there, put when I try to double click on the .py file it is not working. Like the cmd screen goes away in a second.
Do you know why?
(if I open python through cmd it is working, so it is in path and it works)
What is probably happening is that there is an error being thrown on your new computer which causes command prompt to just instantly close.
The best way to run a python script is from an open command prompt/terminal. To do this open a command prompt and move into the directory of your python file. For example, if the file you are trying to run is located at C:\Users\Davide\PythonScripts, then open a command prompt and type
cd C:\Users\Davide\PythonScripts
Now your command window is in the folder that you want to run files out of.
Next you want to tell Python to run your script. This can be done by typing "python ". For example, if your script is name my_script.py, you would type
python my_script.py
What this is doing is telling your computer "open an instance of Python where you are running my_script.py." If there are any errors thrown, the command window will stay open after python closes and you can see what is going on.
Most likely, there is a package you are trying to import which it cannot find because it was not installed on your other computer. If at the top of your file you have "import xxx" or "from xxx import yyy" lines, your other computer might not be finding those modules and just throwing an error that instantly closes command prompt when you just double click the .py file.

Cannot run a Python file from cmd line

I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.
I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).
It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.
Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you

Categories