I'm using the latest spyder/anaconda (Python 3.8) software and a beginner here. When running a simple hello.py script. I get runfile('C:/Users/Raj/CODE/Python Scripts/hello.py', wdir='C:/Users/Raj/CODE/Python Scripts') but nothing else in the console or anywhere saying "Hello World".
def hello():
"""Print "Hello World" and return None."""
print("Hello World")
It can do other print type scripts just fine as I tried an old script from college.
Any function needs to be called in order to get executed. You call a function by typing it's name with the (), in your case hello()
on bottom of the python file add the call to the function hello
hello()
Related
Uhm, this is my first question that I do in Stack Overflow, so let's just go to the point. :)
I'm creating a program with Python v3.6 and it is a like terminal, my plan is do like the programming languages(C#: "console.write"). The commands will be stored in a folder called "lib" and it will have folders to separate the commands, so the thing will be just like this:
------------------------------------------------------------------------------------------------------------------------------------
lib-
console-
write.py
see.py
main.py
------------------------------------------------------------------------------------------------------------------------------------
So, as you can see there's the folder "lib", inside it has the folder "console" and inside the folder has 2 files "write.py" and "see.py". My mission is when the terminal starts the user type a command like: "$ console.write Hello World", doing that the program will separate the "console" from "write" creating a list ["console", "write"]. Now the terminal will see if "console" is a folder, if it exists so the terminal will check if "write"+".py" is a file, if so it will import the file and pass the args "Hello World", the basic structure that the command needs is two lines: "class exec:" and "def main(args):":
class exec:
def main(args):
# The rest of the command here.
print(args)
With everything allright the terminal will import the file and execute the class "exec" and execute the def "main" passing all the arguments to it resulting in:
$ console.write Hello World
Hello World
$
The same to "see.py":
$ console.see /home/user/documents/file.txt
This is a file that contains some text, really interesting.
$
Thx if someone help me. ;) Have a good day!
I have been using Python for a while and have had no problems with the IDE I've used (I've used WingIDE, now I use Spyder).
But when I started testing some code, which I typed on the editor, today, the return statement does not seem to work. Then I wrote even more basic tests which see what return does, and found out that other functions do not work properly as well.
For example for the function,
def test():
return 2
'''the code below is written in the editor'''
test()
print(type(test()))
print(test())
After running the code on the editor.
test() returns 'nothing' (but it's not actually 'nothing/None/blank space')
type(test()) returns 'nothing' (but it's not actually 'nothing/None/blank space')
print(type(test()) prints 'class 'int' '
print(test()) prints 2
But when I type test() on the console, it returns 2, and when I type type(test()) on the console, it returns class: int.
Note that the same results occur before and after I updated Spyder. And previously, the code in the editor functions the same way as the code in the console, as in typing test() in the editor would have returned 2 when I run the code in the editor.
Python/Spyder hasn't done anything this strange before, what happened?
Here's a visualization. Code in editor
Result after running code in editor displayed in the console
Assuming that the missing output of lines 116-118 is what upsets you:
The editor is handling your code as a normal python script which means statements ala test() don't print their result.
That you get the result of something displayed immediately is a special function of the python console and not normal python behavior.
To actually print the result you have to call print(test()) as you did in line 119-120
Update: After some testing in PyCharm
I got the following behavioir:
Copy&Pasting into the python console:
def test():
return 2
test()
>>press enter
indeed results in a blank output, but this:
def test():
return 2
>>press enter
test()
>>press enter
prints 2 as expected. So my conclusion is that Spyder is maybe not displaying the return value because it was executing a multi-instruction script instead of a single operation.
To expand Fabian's N. answer, Spyder has different evaluation models:
Run file, which is equivalent to python my_file.py.
Run line and Run cell, which copy the contents of a line or a cell to the IPython console and run it there. This model doesn't require adding print's to your code.
I know this must be a super basic question, however, I have tried finding a simple answer throughout SO and cannot find one.
So my question is this: How can I execute a python script from the command line such that I can see print statements.
For example, say I have the file test.py:
def hello():
print "hello"
If I enter the interpreter, import test.py, and then call test.hello(), everything works fine. However, I want to be able to just run
python test.py
from the command line and have it print "hello" to the terminal.
How do I do this?
Thanks!
UPDATED:
Yes, sorry, my script is actually more like this:
def main():
hello()
def hello():
print "hello"
Do I still need to call main(), or is it automatically invoked?
Add at the end of the file:
if __name__ == '__main__':
hello()
Your print statement is enclosed in a function definition block. You would need to call the function in order for it to execute:
def hello():
print "hello"
if __name__ == '__main__':
hello()
Basically this is saying "if this file is the main file (has been called from the command line), then run this code."
You have to have the script actually call your method. Generally, you can do this with a if __name__ == "__main__": block.
Alternatively, you could use the -c argument to the interpreter to import and run your module explicitly from the cli, but that would require that the script be on your python path, and also would be bad style as you'd now have executing Python code outside the Python module.
As I understand it, your file just has the following lines:
def hello():
print "hello"
The definition is correct, but when do you "call" the function?
Your file should include a call to the hello() function:
def hello():
print "hello"
hello()
This way, the function is defined and called in a single file.
This is a very "script-like" approach... it works, but there must be a better way to do it
When I import a selfmade module and run the program, the output is what I expected. However when I update the module and run the program in the same console, the previous result is shown. If I open a new console, then the new result is correctly shown.
Let's take an example:
# Filename: myfunctions.py
def helloWorld():
print("Hello World")
# Filename: runfuction.py
from myfunctions import helloWorld
helloWorld()
The output is Hello World. When I replace in myfunctions.py Hello into Bye, and when I run the program in the same console, my result is still Hello World, and not Bye World. The updated text Bye World will be only shown when I open a new console.
try:
reload(module_name)
that's how it works in a local python console. I don't have a PythonAnywhere account, but I would guess it's pretty similar.
Note that any object instances you have already created will not be changed, but this (or something similar) should work fine for functions.
I am interested in a small python application, which can be downloaded here:
https://launchpad.net/treemap
If you run it, like this:
python treemap-basic.py examle-world-population.txt
It works just fine.
The problem is that even if I type a print command in the "treemap-basic.py" file:
print "Hello World !" # treemap-basic.py
I cannot see the message "Hello World !" at the Terminal. Why?
I downloaded this script, and inserted
print "Hello World"
on line 64. When simply trying ./treemap-basic.py on the terminal, you get an IndexError since treemap-basic.py expects a command line argument. When you specify a file to work on:
./treemap-basic examle-world-population.txt
You see a bunch of output in stout. If you scroll up to the top (right below where you first entered the command in the terminal) you should see "Hello World" as the first line of output.