From the course: Using SQL with Python

Installing the tools

- [Instructor] To follow along with the exercises in this course, you'll need a working installation of Python with SQL Lite and MySQL. You'll also need a working development environment for Python. And for this purpose I'm using PyCharm. You can download Python from the python.org website. I recommend that you install the latest version for your platform. And if you bring up their webpage under their downloads link, it'll usually just give you a good big link for the latest version for your platform. You can download the latest MySQL Community Server from the MySQL website at devmysql.com/downloads/mysql. You may choose to run a MySQL server on your development machine, or on a remote machine. I run mine on an old Mac mini separate from my development machine, and that works fine. If you need help setting up MySQL, please see my course, MySQL Installation and Configuration. You can download PyCharm from the jetbrains.com website at jetbrains.com/pycharm/download. The free community edition has all we need. And so I'll be using that. I've already installed these components on both my PC and my Mac. Once you've installed these components, you can open the exercise files directly in PyCharm. You don't want to open it as a new project. You want to just select Open. And here I've got on my desktop, my exercise files folder, and I select Open. You'll notice that it's gone ahead and configured my Python.3.9, which is the latest version of Python. As I record this, your version may be later, that I just installed on this machine. On the Mac, usually it does that automatically. On the PC, it's a little bit different. So here I am on my PC. And I'm going to select Open. And my exercise files from the desktop, I'm just going to say, OK. And you notice it says, No interpreter. It did not configure my interpreter for me. And so I'm going to show you how to configure the interpreter. And this works both on the Mac and on the PC in pretty much the same way. You just come over here and you click on this. And you say, Add interpreter. And you notice that it starts up in this virtual environment. And that is not what you want. It's useful for some purposes, especially where you may want to test your code and different versions of Python. But it is not useful for us. And it adds a layer of complexity that we do not want. And so I'm going to select System Interpreter instead. And it just found my Python Interpreter. And that's where my Python Interpreter is. And I just say, OK. And give this a minute while it does its thing down there. And it has now configured my Python Interpreter. Now, again on both the Mac and the PC, we're not quite done configuring PyCharm. And you know, if you've already been using PyCharm and your PyCharm has already set up and you know how to use it, even if you're using a virtual environment, whatever. As long as it's working, that's all we need. But for our purposes, if you're just setting up PyCharm just for this course, this is the way to do it. Now, if I bring up my from chapter one here, my hello version.py. And I try to run it with shift control F10, or if you're on a Mac, it's shift control R. But here I'm going to press shift control F10. And it's going to just run my Python in the System Interpreter. It's going to run this code. You notice it says, No module MySQL. And here, sometimes this starts out as collapsed. But if you expand this, you see I import mysql.connector as MySQL. And it's not finding that. Because we have not loaded the module. So we need to go and load the module. And there's a couple of ways to do this. You can come up here to File, Settings. And you can select Project Exercise and Python Interpreter. Or you can take this little shortcut down here. If you just click on the Python 3.9 down here and select Interpreter Settings, you get to exactly the same place. And it's a little bit simpler. Okay? And you notice these are all the modules that are loaded in the Python system. Again, we're not using a virtual environment here. And I just want to add one. I want to add mysql-connector-python. This one here. You notice there's a lot of choices here. This is the one that you want. It says version 8.023 for me. It'll probably be a later version for you by the time that you're watching this, and that's fine. But it's the one that says MySQL connector Python. And you need to make sure that you check this box down here that says, Install to user's site packages directory. And again, if you don't select that box, it's not going to work. That sets up this module for the site Python, the system Python on your machine, which is the one that you're using. So I click Install Package. And it says it's done, but it doesn't close the box. So we need to close the box. Then you see it here and you just say, OK. And now when I run this code again, I'm going to close this and I'm going to shift control F10 on the PC. And it's shift control R on the Mac. And you notice that now it runs properly. And it gives me version numbers. This is just my hello world. This is just is my environment working for the development of this project and work purposes for following along with these exercise files. And you notice that I get version numbers for all of these. The version numbers are correct. Yours will probably be different. They'll be a little later. They should be at least these numbers. I've got Python 3.9.4, which is the latest as of the time I'm recording this. SQLite 3.34, that's the version that is coming with the Python as of my recording this. And MySQL connector 8.0.23. Again, the latest version as I record this. Now, if I go back over to the Mac, here I am back on my Mac. And you'll notice if I open up my chapter one and open my hello version.py. And now I'm on the Mac, here I'm going to expand this. It sometimes opens up with that collapsed for some reason. Now my Mac, I'm going to say shift control R. It's shift control F10 on a PC. But it's shift control R on a Mac. It goes ahead and it runs this, and I get the same error, No module MySQL. Okay. I know what to do. I come down here. I click on my interpreter. I say, Interpreter Settings. And you notice I don't have MySQL connector. Again, if you come to your settings, it's right here under your Project Exercise Files and your Python Interpreter. And I'm just going to go ahead and add it. I press this plus here. And I say, mysql-connector, and I have to spell it right, or it does not work. And we find the one that says MySQL connector Python. That all looks correct. I need to check this box. It says, Install to user's site packages. You won't get that checkbox if you're using a virtual environment. That's only when you're using the System Interpreter. But you need to check it and say, Install Package. Again, I get the little green down there. I have to close this and we see that it's installed. And I say, OK. You know, the buttons are on the other side on a Mac. And then I can close this. And now when I run it again, shift control R on my Mac, I get my correct version numbers. That just tells me that our environment is set up and that our environment is working. So just to recap, I can close this down here to run the Python that's opening your window here. On a Mac, it shift control R, and on a PC back over here on my PC it's shift control F10. And that runs your code. So now you're all set up. And you're ready to follow along with the exercise files. PyCharm IDE is flexible and powerful. It has many features and options. I encourage you to explore and learn. It's got a lot of great stuff in it. And you may decide as I have, that this is an editor worth keeping. And I do most of my Python work in PyCharm now.

Contents