Effective PyCharm (2021 edition) Transcripts
Chapter: PyCharm Projects
Lecture: Working with existing projects
0:00 We saw how in PyCharm to create a new project to create the virtual environment
0:03 and get going. But most of the time what we're doing is working with existing
0:08 projects that are completely set up.
0:10 We create project and we might work on it for a year or more.
0:13 Who knows? So if it's already in this list,
0:16 well then that's easy. You click the button,
0:18 it just pops right back open to the way it was.
0:20 But if it's a new project that you haven't opened before,
0:23 let's see how we get that set up.
0:26 I want to start from some existing code that's a little richer and not fiddle around
0:30 with actually trying to create some structure for you.
0:33 We're just gonna work with an existing project and I'm gonna use the python for absolute
0:37 beginners course repository for that. So let's go over here and again.
0:41 I could open it from Git uh VCS for Get but I'll just start from the file
0:47 system that's probably more likely how you're going to do it.
0:50 Let's go to our desktop again.
0:53 I was gonna clone this into the beginners directory and I'm not even going to create
0:58 a virtual environment. Now, what's in here Anyway,
1:01 let's open this up. So we've got code which these are all the applications that
1:05 we wrote during that game. So here's a guessing the number of Eminem's in there
1:09 Here's a rock paper scissors game over here,
1:12 we have like a five or seven way rock paper scissors game some other things tic
1:16 Tac toe. So I want to open this up,
1:19 I would like to be able to go into these sections and run this bit or
1:23 maybe jump over and run this and people to see all of those inside of
1:26 PyCharm. So it's more complicated than just say loading up this directory with this one
1:30 file because I'm on Mac, there's a little bit of a bonus that I get
1:35 that other platforms don't get. I can drag and drop this right on top of
1:41 the PyCharm dock icon and it will open it up.
1:44 If I was running Windows I was running Linux,
1:47 I would just hit open and then browse to that folder.
1:49 But on Mac you can actually do a drag drop trick and I will open it
1:53 up with a bit of a shortcut.
1:55 Now first of all it opened up to read me that it found that the which
1:59 is kind of cool. So you can see all the read me stuff that's in
2:02 here. It also says we're not really sure what's going on.
2:06 We took a guess and chose this Jump Start virtual environment And that's not the right
2:12 one. So I'm gonna go over here and you can see interpreter settings.
2:15 If I open this up that this isn't even valid.
2:18 It's been deleted is long gone.
2:20 So what I want to do is not use this broken one.
2:23 I want to add an interpreter.
2:25 We'll click add interpreter and it says we're going to create a new virtual environment for
2:30 you. Perfect. That's exactly what I was hoping what happened.
2:33 So it's going to be based on 39 and it's going to create it in a
2:36 directory called 'venv'. Which is the common thing that you do in python
2:40 If I had created one through some other mechanism through 'pipenv' or manually on
2:46 the command line as part of my work flow.
2:48 When I did a check out there,
2:50 I could just click this and browse to it.
2:51 But it's going to create a new one for so let's say okay there right now
2:55 everything is looking better. You can see it's over here on the right virtual environment
2:58 right there. Next up let's look at some of these applications.
3:01 So if I have this interactive code thing here we can see we've got a guessing
3:05 game. And if we want to run guessing game,
3:07 we can just run this As we did before.
3:10 And it says, how many Eminem's are in the jar?
3:11 There are seven. Oh sorry,
3:13 that's too low. There's 1000.
3:14 Let's go 200. How about 50 below 75.
3:18 Let's use some binary tree stuff on it,
3:20 85. Wait, no, that was too high.
3:24 I've been disqualified. You have to win this game in five rounds.
3:27 Okay, so you can run this pretty straightforward because it's just this single file.
3:33 Now let's go over to this Chapter6,
3:35 organising code with functions and notice that we've actually got to parts of our applications.
3:42 So we've got our rock paper scissors game here and there's this shared library sub module
3:49 folder. Directory thing named Space.
3:52 I guess you would call it over here and in there there's a UI helper that
3:56 can show the header and here we're trying to show the header.
3:59 If I was running all of this from right here,
4:02 if that was the working directory for PyCharm and python,
4:05 it would just run when I run this particular file.
4:09 However, that's not the case.
4:11 So when we're working with some more complex structure like this,
4:14 we might need to tell PyCharm.
4:16 Look this part, this subpart this right there,
4:20 that chapter boulder represents its own dedicated application and these pieces are used within it.
4:27 So what we can do is we can mark this directory as what's called a sources
4:31 root. So we'll come down here and say Mark directory as sources root.
4:35 And that effectively adds that directory to the python path.
4:39 So when it looks for shared Libs,
4:41 it's going to know well this is part of my path and here's a thing called
4:43 shared Libs. I can go find a.
4:45 UI helper in. So let's do that notice there's other things we can Mark as
4:50 as we could exclude. Like for down here we have our solutions,
4:54 we have some stuff for the read me.
4:55 Here's a bunch of practice things and notice that PyCharm is warning about duplicated code
5:02 in other places and probably that's actually copies of different variations of this app throughout this
5:08 course. But it could be that it's in these other random areas and we can
5:11 actually go over here and exclude those from PyCharms.
5:14 Consideration of source code. Notice that's already happening for our virtual environment.
5:18 That's why it's this orange. So we can go over here and mark these as
5:21 excluded. So that will help.
5:24 Like I said, I think there's still gonna be some reuses these other pieces
5:28 later but it's fewer now. We can also do things like mark this as a
5:33 resource root. So if I'm in a web app and I want to say
5:37 '/static/CSS/site.css' if that structure is at the top of this
5:43 resource route, I'll get auto complete in all sorts of information inside of the html
5:49 files. If I was in Flask or Django and I had a folder that contained
5:54 the templates and then in my Django code,
5:56 my Flask code, I said use this template.
5:59 There's actually a link between the templates that get marked as the template folder and the
6:05 stuff you're doing in python that'll help you navigate back and understand if they exist or
6:09 if they're missing and give you all sorts of warnings.
6:11 So there's a bunch of really cool things that you can do here,
6:14 but probably the most important one to start is this sources route.
6:18 So now I tell PyCharm that this is the application working space and we're gonna
6:23 run this file and it's gonna look in shared Libs or a UI helper that can
6:28 show a header. Alright, so notice the errors are gone.
6:31 If I under this real quick here un market sources root,
6:36 it has come back and it's like this is going to crash,
6:38 it's not gonna work. But again,
6:42 update the python path, we're good to go.
6:44 We'd run it and there we go.
6:47 Here's our little header, see what our little headers doing.
6:50 It says Rock Paper scissor "Function Edition" in right there.
6:54 And that's running this whole thing as a like a sub application.
6:58 Super important. That's how we get started with existing projects.
7:02 A couple of things. We created a new interpreter,
7:05 we added a new virtual environment there.
7:08 We came over here and we said we really want to treat just this section as
7:11 its own inner application. So we mark that as the source route.
7:16 These things, we don't want to be considered as part of our source code or
7:19 for searching or indexing. So we excluded them.
7:22 One other thing is we have a couple of things that we could run,
7:25 you know, so I ran the guessing game and I went and did this and
7:30 I ran the RPS and it did this might get a warning if you want to
7:34 force to restart, which is fine.
7:36 In this case that leads to the names up here,
7:40 you might want to change us.
7:41 We can hit edit. And this will we could call this rock paper scissor game
7:46 and maybe give it some extra information like functions because there's other rock paper scissors scissors
7:50 in here. And this could be the 'MM Guessinggame' like that.
7:58 So now it's a little bit clear.
8:00 Especially if you have the same file like 'app.py' over and over and over
8:04 again. Which one is Which make sure you give it names.
8:07 And then also notice only exit here.
8:09 This is just beginners like over here,
8:12 right click re factor rename. It will say directory or project.
8:17 Project will say 'Beginners Demo Code'.
8:23 Set up here now, but also more importantly,
8:25 in your little list now, you have some little more descriptive beginner demo code still
8:28 in the beginner's folder. All right.
8:30 That's how we work with existing projects.