Mastering PyCharm Transcripts
Chapter: PyCharm Projects
Lecture: Working with existing projects
0:01 Sometimes it's great to create a new project
0:04 and you saw we did that with first project right here,
0:06 but the majority of the time you will be opening existing projects,
0:11 we work on projects and apps and websites for years
0:14 and we only create a few new ones.
0:16 So, let's go and take an existing project and open it in PyCharm
0:21 and MacOS is a little bit of a special treat for us in this regard,
0:25 compared to say Windows or Linux, it will make it just slightly easier
0:28 so let's go back over here and do another git clone
0:32 and this time I want to clone my Python Jumpstart course's demos
0:38 and the reason is, if you look in here the apps
0:40 you'll see there's all sorts of different applications or projects in here if you will
0:46 and within each one, they actually have two,
0:48 every one has a final and a you_try so you can try it out.
0:52 In here, you'll see there's actually a lot of sort of sub-pieces
0:56 and that will let us explore some of the more nuanced bits of PyCharm.
0:59 So what I am going to do, is go over here and git clone that,
1:01 as Jumpstart to my desktop— done.
1:07 Now, I want to open up this entire project here
1:11 and then have this apps navigable within my PyCharm instance.
1:17 Now you could decide to just come over here and just open up this
1:20 lets pick something more interesting,
1:22 just this one which has the sort of wizard game
1:25 and you could open that up and you could rename it so it's not called final
1:28 but if you want to do something more holistic,
1:31 like I want to open up all the Jumpstart stuff,
1:33 that's what we're going to assume you want to do here.
1:37 So if I go over here and I drag this on the PyCharm in MacOS
1:42 it will actually open it in PyCharm.
1:45 If you want to do that in Windows or in Linux,
1:50 you have to do open and then browse to it,
1:52 it's not a big deal but this little drag and drop is nice.
1:54 However, either of those actions, you don't want to do them just yet;
1:57 what I want to do is I want to create a virtual environment
2:00 and use some conventions that PyCharm will detect for us
2:04 to help set up this environment,
2:07 so I already showed you when you create a new project
2:10 how you can set the environment,
2:12 you can do that internally from some of the settings
2:14 after you open up the project, we'll talk about that later,
2:17 but for now, let's see sort of what I think is the best workflow here.
2:20 So I just checked this out from git,
2:22 git will not have the virtual environment with it
2:25 virtual environments are tied to your machine,
2:27 your Python version to your OS,
2:29 so MacOS won't run say a Linux one or a Windows one.
2:36 So you don't want to check that stuff in,
2:38 so I need to recreate it because I just checked it out
2:40 so I am going to go to Jumpstart, and I'm going to go to Python3 -m venv,
2:46 now on certain versions of Mac and certain versions of PyCharm
2:50 intersected along with certain versions of Python
2:52 there's this thing where some of the symbolic links
2:56 are not quite detected as links and are followed
2:59 and this doesn't quite work right, so I'll do --copies
3:02 it's not always required but on Mac it can help with some things.
3:06 So I'll say create a virtual environment in the folder .env
3:11 at the root of the folder I'm going to open in PyCharm, which is Jumpstart.
3:18 Okay, great that's done.
3:20 Now, what I can do is either do open directory
3:23 or like I said on Mac drag and drop here and it opens up
3:26 and we see it's thinking for a minute, okay, it's all happy now,
3:31 come over here and you can see here's our .env that we actually just created
3:36 and the question is if I were to run something,
3:40 let's go find some of our files here, let's just run Hello world,
3:43 final not super exciting, but let's run it.
3:47 You can notice right here that PyCharm actually auto detected that,
3:51 it detected that we had a .env folder that was a virtual environment
3:56 in the top level directory of the one that we opened.
3:59 So I recommend that definitely
4:01 as a way to start into existing projects is to have that there,
4:04 once you've opened it, you can't go and put it back,
4:06 it won't detect it again, because it creates some other files
4:09 that sort of say what it's supposed to do after you've credited it.
4:12 The very first time this little trick is great.
4:15 So now we've got this open, you can see
4:17 we've already got our run configuration here
4:19 because I right clicked and ran it, and it asked me what my name is,
4:22 my name is Michael, nice to meet you.
4:24 Okay great.
4:26 Now, that seemed like everything works great,
4:28 but there's a few other details here that we need to deal with,
4:32 like this wizard battle for example, the program imports actors
4:37 and you can notice that PyCharm is already like,
4:39 hmm, not so much, I don't know about this,
4:42 let's just say we're going to give it a try, this is probably going to work,
4:46 let me try to run this and I'll show you to rename that better,
4:49 so if I run it and I say attack, it totally worked, how about that?
4:57 So, even though PyCharm doesn't know that we are able to import actors
5:01 the system did, and what's going on here, why didn't that work?
5:05 So the reason this didn't work is PyCharm thinks of this whole thing
5:10 as one Python project, the way we are working right now.
5:14 And so when I say import actors it goes to the top
5:16 and it looks for like an actor's thing up here, obviously, there is none.
5:20 So what we can do for each one of these is
5:23 we can go here and say mark this as a sources root
5:27 say basically this is the relative path that Python is going to use
5:33 when we run it and so that's what PyCharm should use
5:39 to analyze things like this from actors, notice that error went away.
5:43 So I can come over here, this one has a bunch of different things
5:47 so I could say mark that as sources root,
5:51 and over here this stuff can import the various pieces
5:54 like journal without any errors.
5:57 So this ability to configure the structure is really nice
6:01 if we were in say a web application, come over here and say
6:05 maybe there's like some kind of a static directory like that,
6:10 and then here we're maybe going to have a style sheet,
6:14 this will be site.css, so down here we'd like to import this,
6:18 but PyCharm doesn't really know necessarily
6:23 where to start and stop in this hierarchy,
6:25 so what we're going to do is we are going to go over here
6:27 and we'll mark the top thing as a resource root,
6:31 and so that way if I ever try to say /static/site.css in a web app
6:37 it's going to know to look at all these sources roots.
6:40 So when we create these big projects,
6:43 you probably want to take a little time to go through here
6:46 and mark the directories as the various things,
6:49 so if you're doing like Jinja 2 templates or Chameleon templates,
6:51 you can mark this as a template folder,
6:53 we don't have any here so that doesn't make any sense,
6:57 but this one we might exclude this environment
6:59 and that tells PyCharm to not look there, to not index there,
7:02 to just treat that as just stuff hanging around.
7:04 Similarly, we have all these transcripts here which is like tons and tons of text,
7:09 so this we can say don't look in here for any of your indexing or stuff like that,
7:14 we want to keep it simpler, alright.
7:16 So now we just have our apps going like this with our sources root.
7:20 Here is how you open a new project,
7:23 and you kind of get it configured in the way that you want
7:26 so this way with PyCharm I can still get to all of the files
7:28 but I can also tell it like, "Look here is where the code actually is,
7:32 this is the part we're going to run."
7:34 Now, when I ran, we saw we had these configurations up here
7:37 and the names were a little weird, so let's call this "wizard app",
7:41 and we can also edit the other one that's called "program"
7:46 we could call this "hello world".
7:49 And then if I want to run it, it's super easy to tell which one am I going to run,
7:53 "wizard app" or "hello world"— let's run "hello world". "Hello world."
7:58 We want to run "wizard app", beautiful.
8:02 Let's attack, great, "wizard" won the battle.
8:08 So let's just review real quick— if we are going to open existing project
8:12 we can do that by just dragging and dropping the folder in MacOS
8:15 or open directory with the file menu or the little icon in the others.
8:20 And once you get it created, once you get it opened,
8:23 you probably want to configure the directory,
8:25 so PyCharm understands here's where the source files live,
8:28 here's where the static files live in say a web application, things like that.
8:32 We created our run configurations by right clicking the running
8:36 and then once we got them up
8:38 we didn't want to just call them program 1, program 2,
8:40 we gave them some nice names and now they're ready to run,
8:43 or debug or whatever we need to do.