Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python What is Python?

It doesn't work (nothing prints)

Ran this bit of code:

name = 'Megan'

print('My name is')
print('Megan')

however nothing prints?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Can you show how you ran this code?

Save code to file, say, code.py

Run python code.py

Christopher Lorenz
Christopher Lorenz
3,183 Points

name = "Megan"

print("My name is") print(f"{name}")

8 Answers

Hi, Thanks for getting back to me.

Sure, I typed the code as exactly as you have it above. Then the instructor said to:

  1. Hit 'Ctrl + S' (since I'm on a PC) in order to save.

  2. Type into the bottom (Console) window:

python app.py

So the full line looks like this:

treehouse:~/workspace$ python app.py

  1. Hit "Enter" key

-at least that's what the instructor said to do.

To be honest, I would be a lot more comfortable in Microsoft VS Code if that were an option. Oh well.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Christopher de Grasse, here's what happens when I save your code to the a file named app.py:

$ cat app.py
name = 'Megan'

print('My name is')
print('Megan')

$ python app.py
My name is
Megan

Check out the help for running Python on Windows.

What do you get when you simply type:

  • python
  • py

Do you get the python REPL? (if needed, type exit() to leave REPL)

Post back if you need more help! Good luck!!

Ok, I found out the issue. It looks like my company is running some "Code 42 Software" and a bunch of other junk that prevents workspace from functioning correctly. I ran the same code on my personal laptop, and it runs fine. Thanks for the help!

If I type it into the Console, I get:

treehouse:~/workspace$ python                                                                                                                                                    
Python 3.9.0 (default, Jan 21 2021, 00:46:14)                                                                                                                                    
[GCC 5.4.0 20160609] on linux                                                                                                                                                    
Type "help", "copyright", "credits" or "license" for more information.                                                                                                           
>>> py                                                                                                                                                                           
Traceback (most recent call last):                                                                                                                                               
  File "<stdin>", line 1, in <module>                                                                                                                                            
NameError: name 'py' is not defined                                                                                                                                              
>>>
Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Ah, when you say you’re running on Windows you do not mean in a Windows cmd shell window. You are running in the workspaces within a browser window. For future reference, the workspace console is running a version of Linux. This is typically denoted by the prompt ending in $.

What is the name of the saved .py file?

Assuming it is app.py, what happens when you type:

treehouse:~/workspace $ ls -a

treehouse:~/workspace $ python app.py

I tried typing the same code into VS Code. - It ran flawlessly, so some issue in "Workspace" environment?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Perhaps, that’s why I’m hoping to resolve what might be wrong with the workspaces setup.

Ok, so DON'T type into the lower portion of browser based Workspace?

This is what the code returns (Console section of browser based Workspace):

>>> ls -a                                                                                                                                                                        
Traceback (most recent call last):                                                                                                                                               
  File "<stdin>", line 1, in <module>                                                                                                                                       
NameError: name 'ls' is not defined                                                                                                                                          
>>>                                                                                                                                                                              
KeyboardInterrupt                                                                                                                                                                
>>>                                                                                                                                                                              
KeyboardInterrupt                                                                                                                                                                
>>>                                                                                                                                                                              
KeyboardInterrupt                                                                                                                                                                
>>>
Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Notice the difference in the prompts:

>>> is the REPL prompt. Use exit() to return to the shell prompt.

$ is the Linux shell prompt

Type the ls -a and python app.py at the shell prompt.

>>>
python app.py                                                                                                                                                                
  File "<stdin>", line 1                                                                                                                                                         
    python app.py                                                                                                                                                                
           ^                                                                                                                                                                     
SyntaxError: invalid syntax                                                                                                                                                      
>>>
```Python

None of the code you mentioned will run from a command prompt, presumably because some Environment variable would need to be set to point to the location of the .py file??

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

It appears you are running shell command within the Python REPL.

Running completely in the console, from a $ prompt:

treehouse:~/workspace$ python                                                                                                                                                    
Python 3.9.0 (default, Jan 21 2021, 00:46:14)                                                                                                                                    
[GCC 5.4.0 20160609] on linux                                                                                                                                                    
Type "help", "copyright", "credits" or "license" for more information.                                                                                                           
>>> name = Megan
>>> print(name)
Megan
>>>

The last line will print to the screen.

I think there is some confusion to running code interactively in the REPL verses executing a python file from the shell prompt.

The REPL is started at the shell prompt using $ python where $ is the prompt.

A python file is executed from the shell prompt using $ python filename.py