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