How to run a python program in Windows 10 command prompt - python-3.x

I am getting as such error for executing my hello world program:
"This file does not have a program associated with it for performing this action"

1) Download the official version of Python
2) Use custom installation following the screenshots bellow.
3) Run your hello.py file directly from the command line by typing hello.py
Screenshots #1
Screenshots #2
Screenshots #3
I have this python code that I'm running on just by typing Q60471933.py
from datetime import datetime
print("\nHello World\n",datetime.now().strftime("\b%m/%d/%Y\n%H:%M:%S"))
Outputs:

Try this in your command prompt:
python3 hello.py

Related

Syntax errors: python 3.8

I have this error using python 3.8 from the interpreter
>>> python hello.py
File "<stdin>", line 1
python hello.py
^
SyntaxError: invalid syntax
>>>`
I have attempted several changes in the editor using python or env python or python3
what am I missing? Thx in advance
You are trying to run your code while being in the "python shell" A.K.A Python REPL
You need to exit from the shell and try running your code again.
You can quit by typing quit() or by pressing Ctrl-Z on your terminal
Then you can type in python hello.py or preferably python <directory_to_file>/hello.py to run your code.
On side note:
Try looking up a tutorial on YouTube, or reputable websites on learning python.
Try using a popular editor, such as VS Code or PyCharm to enhance your coding experience.

Python script to launch pymol on windows

I have installed the opensource version of pymol on windows (https://github.com/schrodinger/pymol-open-source).
To run pymol, I can open a python3.8 command prompt on windows and type the following:
import pymol
pymol.finish_launching()
This launches the pymol gui.
I'd now like to create a simple python script so that I can click the script (or convert to a windows executible using for example pyinstaller), however if I put the above commands in a script pymol.py and then run with:
python pymol.py
where python points to python3.8 I get the following error:
AttributeError: partially initialize module 'pymol' has no attribute 'finish_launching' most likely due to a circular import.
Why does this work on the command line python but not in a script and how can I fix this so the script runs and can be converted to a windows executible?

Python Import Error: No module named mysql.connector

I am running into a problem with mysql.connector in Python 3.7 on Windows. I don't know if it's a conflict with the Python 2.7 installation that I did a few days ago. The original script was working fine last time I checked (more than a month ago). The system variables seem fine. Python 3.7 is there.
I isolated the issue to demonstrate better what is going on.
When I attempt to import mysql.connector with IDLE it raises no issues.
IDLE import mysql.connector
Now, with a simple script
import mysql.connector
print("Hello!")
input("Exit.")
again it's fine if I run it with IDLE editor:
Run script with IDLE editor
In fact, the original script runs as intended with IDLE editor.
But, when I attempt to run the script from cmd with 'python test.py' I get this error:
CMD execute script
Of course, when I try to open the script by double-clicking on it, the window opens and closes immediately. I'd appreciate your help on this issue.
First check your python version using python -V if its not 3.7, then use below to execute code from cmd.
python3 test.py

python script fails in terminal via 'import psutil'

I wrote a simple script to check the battery time on my laptop (the built in one on xubuntu seems somewhat inaccurate), the code runs fine in a python terminal but when I run it without starting in the python terminal I get an error related to 'import psutil'.
the script is
I'm very inexperienced with python and haven't been able to find anything helpful online.
#!/usr/bin/env python3.6.7
import psutil
import time
a=psutil.sensors_battery()
b=str(a)
c=b[b.rfind(start)+len(start):b.rfind(end)]
d=int(c)/3600
print(d)
time.sleep(130)
exit()
what I expected is the value c divided by 3600 to be printed on the terminal, display for a while and then exit. instead i get
Desktop$ python battery.py
Traceback (most recent call last):
File "battery.py", line 2, in <module>
import psutil
ImportError: No module named psutil
i also tried running through the menu options when i right click the script and select python3.6, where a terminal flashes and closes immediatly (to quickly to read any of the print in it).
i've also changed
#!/usr/bin/env python3
#!/usr/bin/env python3.6
#!/usr/bin/env python3.6.7
each has the same result
thank you for any advice
david

Finding IDLE in Python 3

I am running python 3.3.5. I can't figure out how to launch the IDLE IDE. I installed everything correctly but the IDLE doesn't appear anywhere.
To start it from the command line in Python 3.3+:
$ python3 -midlelib
Or in the code:
import idlelib.PyShell
idlelib.PyShell.main()
You will find an idle.py file at C:\Python33\Lib\idlelib.
There are 2 idle.pys file and both of them work. The first opens a command prompt alongside idle and the other doesn't.

Resources