Music: Living Voyage by Kevin MacLeod is licensed under a Creative Commons Attribution 4.0 license. creativecommons.org/licenses/by/4.0
Starting a Terminal and Learning New Commands
00:00 So in the previous lesson, we’ve identified and hopefully fixed the bug. Now in this lesson, I just want to move over to the editor to set up the project properly so that I can then also run it in the terminal myself.
00:13
So I’m going to go ahead and create a new terminal and then do the setup for uv, because so far I was just running it through the global cached uv, but I want to actually have a local .venv.
00:25
So I’m going to create that by saying uv venv, and then it sets up a .venv/ folder for me. And then I will also run uv sync to install the dependencies.
00:40
And now I should be able to run the project using uv run uvicorn (unrelated, those two, by the way) And main:app Just give it the default localhost.
01:00
Oh, it’s getting an error. Could not import the module main. So let’s see what’s happening here. Do some more debugging. I do want to have a new chat for that, because the rest of the context should be unrelated.
01:12 So I’ll add that to chat. As you can see, that’s also a convenient feature. If you get an error message in your terminal, then you can just press Command + L or Control + L, or just press the button and add it to the chat.
01:24 I’m not even going to say anything else here. Let’s just see how it debugs that.
01:34
So there you go. “The main.py file is in the app/ directory, not at the root. Use app.main:app to run it.” Okay, so this– it’s running it for me.
01:44 I don’t want it to do that right now. I’m on the path of doing it myself, so I stop it. It’s also something you can always interrupt any of these runs. So close it again. I got my answer of what was going wrong.
01:56
So I can just go ahead and say uv run uvicorn app.main and then run the app from there.
02:06 All right. It’s looking a bit better, but now we get a problem because the address is already in use. So this is probably because Cursor is running–running it somewhere else.
02:15 Yeah, we can see it’s still over here. So I need to make sure to stop that first. Again, let’s see what we can get here. If I add that to– ah yeah this is probably because it started it right here, right?
02:29
And then I canceled it. But who knows what’s going on. “already in use”. All right, so it’s, it’s showing me lsof is checking on what’s running currently on the port, and there are three processes running, so it’s killing all these three processes, and then lsof comes back empty.
02:49 So nothing’s running on port 8000 right now. And then it went ahead and ran it for me. So it’s because I have it set up to just auto-accept and run everything.
02:58 I could change that so that it asks me before doing those things, but that’s okay. What I’m going to do though is I’m going to snatch this command and I still want to do this by myself.
03:10 So I’m going to stop the server here,
03:14 correct this… to stop the server, find the process,
03:19 or kill it like that. Let’s see if we’re out of server now. “Agent terminals are read-only.” Okay, so I’m going to kill this terminal and another one that I don’t need around.
03:31
Bear with me. We’re getting there. All right, so I’m back to my own one and give this another run: run uvicorn main:app --reload. Address is already in use. ls, let’s see if I remember what I learned. I can use lsof -ti I think. Is that right?
03:54
Yes. So lsof -ti 8000 and then we should see there are some processes running again, because it started off something here. So I can kill those processes just using kill -9 and then put in these process numbers.
04:17 Now this may be a little around the block, what I’m doing here, but my point when I’m working with these tools is not only just to get things done, but also I want to learn something.
04:26
This is a new command for me to figure out that I haven’t really used before, the lsof command, but now I’ve learned about it and I can see what it’s doing with it, right?
04:36
And so I want to practice typing it a bit. And now we want to go uv run uvicorn. Try it again. Whether the reload is working now.
04:48
“Could not import module main.” Did I run the wrong one? main:app again, I forgot to– look at me, but that’s what I’ve learned. I can do app.main:app --reload.
05:02 And so here I am applying the concepts I’ve just learned until we get there. There you go. We now have it running again. I should be able to reload this browser over here
05:13 and get the auto-generated documentation. I should still have all the items in there. Let’s look at that. Try it out.
05:22
Successful response. Content length 2. Ah, there’s nothing in there. So it seems like this was also running a database that was running inside of the uv-managed environment.
05:33 So that’s also why we don’t have a database file here. So now that I’ve set up the project properly in my project folder, I’m going to start again by creating two items.
05:44 OK, let’s remake the two programming languages, Python and then JavaScript.
05:56
And I should have two items created, two IDs, and we also see that now on the side I have items.db. So this is the SQLite database that now popped up.
06:06
It was before living in wherever this project was run from, managed by uv. Okay, and it looks like this is maybe still in the wrong order. Let me try that out. If I execute it, I still get that.
06:18
Oh no. Okay, so it’s ordered, it’s just showing up here, higher up, but in main.py,
06:25
the– right, it’s deleted from down here. Instead, we have randomized sitting here towards the top in line 50 onwards before we’re trying to get item by ID, which happens then next.
06:45 And so this order was the problem before, and it got fixed. Indeed, I did a couple of detours to actually start the server by myself. And now it is working as expected.
06:57 I can execute this a couple of times. Let me get rid of the terminal where you can see that if I execute, you see Python comes first and JavaScript, and then JavaScript comes first and then Python.
07:09 All right, I just had to execute it once more so that Python comes up first. And with this, we’ve confirmed that the bug is fixed correctly, and also learned a bit about terminal commands and about other possible ways of debugging if you’re running into troubles while developing with Cursor.
Become a Member to join the conversation.
