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
Andy Christie
1,361 PointsPython console keeps closing after I open a script in it.
When I tried opening a .py file the console opens and then closes straight after so I tried opening it in an open console with python and then the file path by dragging and dropping the file in to the console but it says invalid syntax?
Andy Christie
1,361 PointsHi thanks again Iain but it's the python 3.4 console terminal that keeps closing.
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsIf you are trying to open the console with python somefile.py, this will not open an interactive session but rather will try to execute the file somefile.py. To execute a file and open an interactive session uses "-i":
# display test file foo
$ cat foo.py
d1 = {'a': 1, 'b': 2}
print(d1)
# execute file using python
$ python foo.py
{'a': 1, 'b': 2}
# start python interactive and execute file
$ python -i foo.py
{'a': 1, 'b': 2}
>>> d1
{'a': 1, 'b': 2}
>>> quit()
# Start python interactively first then import file:
$ python -i
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
{'a': 1, 'b': 2}
>>> foo.d1
{'a': 1, 'b': 2}
>>> quit()
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsCan you share the particular Workspace (if that's what you're using) with a Snapshot?
If not, can you share the code of the
.pyfile you're trying, and let us know what operating system and console/terminal application you're using?