Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Modeling

Running Database from Interactive Console in PyCharm

I am trying to use PyCharm to run the coded on the Database Modeling video but I can't seem to get the Interactive Console to cooperate. I believe I don't have the path set up correctly, but I've spent hours searching youtube and the internet for instructions on how to set up the path correctly without success. I really don't want to rely on workspace if I don't have to. I have also tried this with Visual Studio without success. If anyone knows how to set up a path or can direct me to a source that could provide that information, it would be greatly appreciated. Thank you in advance!

My directory: FirstTry (H:\Code\FirstTry) src database.py

I included a folder for src - based on the set up instructions advised by the PyCharm. I can get rid of it if it matters, but I got the same errors as when I excluded it.

I call the file database.py while in the video the file was students.py


The code I entered is the same code in the video:

 from peewee import *

db = SqliteDatabase('Student.db')

class Student(Model):
    username = CharField(max_length=255, unique=True)
    points = IntegerField(default=0)

    class Meta:
        database = db

if __name__ == '__main__':
    db.connect()
    db.create_table([Student], safe=True)

Now when I enter the same commands in the Interactive Console as the teacher does, I can't seem to load the file from the editor:

The teacher entered python students.py where I entered python database.py

For some reason my Interactive Console prompt is In[1] or in[2]...ect... instead of >>>


I type the following at the prompt:

In[5]: python database.py

I receive the following error message:

  In[5]: python database.py
  File "<ipython-input-5-6dc255c5ddc6>", line 1
    python database.py
              ^
SyntaxError: invalid syntax

Sorry, the directory is as follows: My directory:

FirstTry (H:\Code\FirstTry) src database.py

I included this folder called src after reading the PyCharm set up instructions. I can get rid of it if it's confusing. I ran the file without it and got the same error message.

2 Answers

Script Code
Script Code
6,631 Points

Your code is good, but I noticed that you are using Pycharm's console. What you need to be using is Pycharm's terminal (terminal != IDE). The console is an IDE comparable with Python's Shell; IDLE. That's why you are getting a syntax error.

To get the sqlite3 bit working, I would suggest you do the following: download sqlite3.exe from: https://www.sqlite.org/download.html

Option 1 (easy):

Copy the exe file to the same folder where your student.db file resides. On the terminal run the following commands:

  1. python students.py
  2. dir

You should now see the students.db file.

Now you can try sqlite3 students.db

Option 2:

add the path of the sqlite3.exe directory to the environment variables. run the program from the command line.

If you want to know more about Pycharm's terminal go to:

https://www.jetbrains.com/help/pycharm/2016.1/working-with-embedded-local-terminal.html

Hope this helps. Good luck, and Happy Coding

Sebastien Simonek
Sebastien Simonek
3,073 Points

Hi,

Try db.create_tables([Student], safe=True) instead of db.create_table([Student], safe=True)