I have a seemingly simple task to do. Run this ( print('Hello World') ) line of code in PyCharm. No, I'm serious. I wont bother complaining about what I've tried or how much I've coded in the past (spoiler alert, a lot) because I just want to get it running. The online tutorial wants me to do some kind of project structure thing before I can even start, which didn't even work to begin with. I really just want to be able to run code from a file, so if anyone who's figured it out can tell me in a
"
Do this
then this
finally this
"
type format that would be wonderful, because I cant even run a single line from a file.
Well it's really simple, if you used any code editor in the past you probably will understand what i'm doing here:
File >> New >> Python File >> "name of the file" >> Create.
To run the code simply click the play icon which is run 'project'.
Then the output should appear in a console.
I did this:
Create folder wherever you want your file to be
Open this folder in pycharm as your project (if you have a project open, close it and open this folder as your new project)
Open Project tab on the left
Right click your folder -> New -> Python File -> Create hello.py with print 'Hello World' as contents
From the top menus, go to Run-> from the drop-down click Run
Click hello so it knows to run that.
If you don't want to deal with the Run stuff you can always invoke your script from the command line with python hello.py.
Hello World! should appear in your console!
Related
I need to open hello.py file in a new console window from another python file basemain.py
I need the hello.py file to pop open in a new python console. I am not just trying to read, run or execute the file. I know I can manually open the file with ctrl+o and open it in separate window but I am trying to use code instead of me using ctrl+o every single time.
I have already tried
1)
from subprocess import Popen
#Popen(['python3', 'C:\\Users\\sammy\\Desktop\\project\\hello.py'], shell=True)
did not work. I just saw a glimpse of command promt for 1 second then it went away. I did not see file popped up in a separate console
2)
open('C:/Users/sammy/Desktop/project/hello.py',"r")
also didn't work. I am guessing the file is being read but I need to see it popped up in a separate console. that didn't happen
3)
import os
webbrowser.open('hello.py')
just opened the internet browser. nothing else happened
4)
os.system("hello.py")
again, same thing as 1)
5)
import os
os.startfile('hello.py')
again, same thing as 1)
6)
import webbrowser
import os
webbrowser.open(os.getcwd() + "C:/Users/sammy/Desktop/project/hello.py")
again, just opened internet browser. nothing else
These are the solutions I found in other questions in stackoverflow and in other forums. none of them are making a separate console to be popped up so that I can see and edit the file and then run. some of them are just reading the file but I cannot see the file
I am using python 3.10 in Windows 10 system
I am fairly new in python, so if anyone can help, I greatly appreciate that. Thanks in advance
I have written code in a New file window in IDLE.
When I run the code there is no output.
Instead a dialog box appears showing a window accessing Python Folder 37-32.
When i closed the dialog box and the file I tried to create a new simple code below but I when I ran the code I got the same Dialog box.
What is wrong?
sum = 2+3 print(sum)
I have attached a screenshot showing the code and the dialog box that appears when the Module is run
Before you can execute your code, your first need to save the file. Thats the dialog box that popped up.
You should have seen a popup box like below. Did you? Is is unclear?
Save Before Run or Check
? Source Must Be Saved
OK to Save?
[OK] [Cancel]
One reason to require saving is that exception tracebacks refer to the file and line of lines that lead to the exception.
If you had saved, sum = 2+3 print(sum) would be a SyntaxError.
You can run single statements in Shell without saving.
The RESTART lines says that the shell re-initialized the environment for executing your code.
You should normally not save your code buried in the installation Scripts directory. Better to make a directory in your user directory, for instance, C:/Users/yourname/py/.
Yes, one should usually open a new question for unrelated questions. But without access to your machine, it is hard to know what happened with 'new'. It may be that IDLE could create a file under .../appdate/.../Scripts/, but your code cannot. If the open call did not raise and exception, it was likely created somewhere. Until you are more experienced, better to use absolute paths, such as C:/Users/yourname/py/new.txt.
I want to add a systemtray to an existing python based project: https://github.com/piejanssens/premiumizer
How exactly do I need to do that? I am completely new to python and I am using it for the first time, just because I want to add a little feature to and existing script.
What I want to achieve is that when the script is running that there should be a system tray icon which opens http://localhost:5000 if it is double clicked. And if it is right clicked there should be an Exit/Quit option.
I have researched a bit and I think I could achieve it with one of these two scripts https://github.com/moses-palmer/pystray or with https://github.com/Infinidat/infi.systray (I also read that infi.systray should be used because it is not dependent on pywin32 because it uses the ctypes library because that one is part of the standard Python library).
So I tried to add this code for testing to the premiumizer.py file:
from infi.systray import SysTrayIcon
def say_hello(systray):
print "Hello, World!"
menu_options = (("Say Hello", None, say_hello),)
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
systray.start()
But now the Console is closing itself immediately. How can I check what went wrong? Is an error log saved somewhere ?
What do I need to do to make it work? Or is there an easier way for someone "stupid" like me ?
Welcome to the world of python!
Let me assume that you copied the script you've posted into a python file and just ran the file, correct? If so, the problem is that once the script is executed the program exits and with it the tray icon.
Start an interactive console by running python or (ipython if you have it installed) in a command window and paste in your code. You'll see that the tray icon appears and stays. It disappears once you close the console.
(Remark: the code above uses the python 2.x version of print without the () and will cause an error in python 3.x, there use print("Hello, World!").)
To make this work you need to put this code somewhere in the setup/initialization part of the premiumizer. Without knowing this project I cannot be of further help where to exactly.
The read me says:
Copy IDLE2HTML.py to your the idlelib folder of your python
installation. Append the contents of 'config-extensions.txt' to your
'config-extensions.def' file (also in the idlelib folder) and restart
IDLE.
For more information on extending IDLE read 'config-main.def',
'config-extensions.def' and 'extend.txt'
Here is the code I am supposed to append from the txt file to the def file:
[IDLE2HTML]
enable=1
[IDLE2HTML_bindings]
idle2html=
My attempts to search for an answer led me to this post that had this code:
with open("test.txt", "a") as myfile:
myfile.write("appended text")
but I had a few questions:
Would that code work on a .def file or would only work for a .txt file
The text I need to append is 4 lines, would I just have to run it 4 times using each of the four times as an input?
Is there a way to append the .txt file directly on to the .def file?
Thanks in advance to any help I can get, I realize this is probably a very noob-y question but I am learning Python by following an online class and I have yet to learn how to work w/ reading and writing files.
You don't need to do this programmatically. What the installation instructions are actually saying is:
1.) Navigate to the folder where idle.py is saved. On Windows this should be at: C:\Python27\Lib\idlelib or something similar. If you're having a hard time finding it on Windows you can right click on it in the Start menu and navigate to the Shortcut tab - this should show you where it is located. If you're on another OS I'm sure a quick Google search can help you out.
2.) Copy the IDLE2HTML.py file that you downloaded to this folder
3.) Within this folder there should also be a file called config-extensions.def. You can open this with Notepad (or any other simple text editor - just use right-click and "Open with") and copy/paste those four lines onto the end of the file and save.
If all goes well you should see the new behavior once you restart IDLE.
If you can start IDLE, you can find the path to its directory as follows: on the top menu, click File, then Open Module. (The shortcut is Alt-M at least on Windows.) Enter idlelib. The title bar of the editor window will contain __init__.py - <path-to-idlelib>/__init__.py (x.y.z).
I'm currently using PyCharm and Sublime Text in Window to develop some Python script, but this morning something quite strange happens.
I am changing my code and testing it by running my code over test input. It is supposed to change the output text quite a lot. The output text at the end did not change at all. Then I try commenting out my output function, simply pass through the reading script. I thought "Now it should print nothing". But it prints the same output as yesterday, as if I never modified it today.
Any suggestion?
It turns out it's because my colleague has pushed an unexpected change up to repo last night...I always pulled before I end the day. The text output was forced by his logger:)
Mystery solved! Thanks for reading guys.
I've had exactly the same thing happen with Sublime. In my case I had moved / renamed the file after closing sublime. I reopened sublime some time later which reopened the old version of the file because I had left the file open when I last closed down sublime.
Have you tried closing the file and then opening it again from windows i.e. right-click => open with Sublime Text, to make sure you're editing the correct version?