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 trialChristopher de Grasse
35 PointsIt doesn't work (nothing prints)
Ran this bit of code:
name = 'Megan'
print('My name is')
print('Megan')
however nothing prints?
Christopher Lorenz
3,183 Pointsname = "Megan"
print("My name is") print(f"{name}")
8 Answers
Christopher de Grasse
35 PointsHi, Thanks for getting back to me.
Sure, I typed the code as exactly as you have it above. Then the instructor said to:
Hit 'Ctrl + S' (since I'm on a PC) in order to save.
Type into the bottom (Console) window:
python app.py
So the full line looks like this:
treehouse:~/workspace$ python app.py
- 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
Treehouse Moderator 68,454 PointsHey 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!!
Christopher de Grasse
35 PointsOk, 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!
Christopher de Grasse
35 PointsIf 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
Treehouse Moderator 68,454 PointsAh, 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
Christopher de Grasse
35 PointsI tried typing the same code into VS Code. - It ran flawlessly, so some issue in "Workspace" environment?
Chris Freeman
Treehouse Moderator 68,454 PointsPerhaps, that’s why I’m hoping to resolve what might be wrong with the workspaces setup.
Christopher de Grasse
35 PointsOk, 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
Treehouse Moderator 68,454 PointsNotice 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.
Christopher de Grasse
35 Points>>>
python app.py
File "<stdin>", line 1
python app.py
^
SyntaxError: invalid syntax
>>>
```Python
Christopher de Grasse
35 PointsNone 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
Treehouse Moderator 68,454 PointsIt 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
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsCan you show how you ran this code?
Save code to file, say,
code.py
Run
python code.py