1

so I'm new to Python and Flask and I'm currently playing around with some CRUD-statements within Flask/Python

I want to know if I fully understand what's going on but I'm a little bit unsecure regarding the following topic: Modules, Packages import

I want to connect to my SQLite database with Flask. Doing so, I have to do some imports:

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

First thing after the imports are done is to set a basedirectory (=basedir):

basedir = os.path.abspath(os.path.dirname(__file__))

And regarding those steps I have some questions:

Question:

import os
from flask import Flask

Does the first import ("import os") mean that I'm only using a Module called "os"? It's a standalone .py - "file" including a class, some attributes and methods, right?

Does the second import ("from flask import Flask") mean that I'm using the package "flask" and import the module "Flask"? If, e.g., there would be another import like "render_template", does that mean I'm using this module or is it a method from the module "Flask"?

Second question:

basedir = os.path.abspath(os.path.dirname(__file__))

I'd like to understand this code. First of all, I'm declaring a variable called basedir. Then I am going to set the value of that variable to the absolute path for the current .py-script. Now to the single steps:

os => means that I'm using the already imported module "os", right? path => means that I'm using an attribute from that module? abspath => means that I'm using a method within the "os" module called "abspath(value)"? The next thing would be clear if I get an answer to the other things: "

(os.path.dirname(__filename__))

__filename__ => that's a built-in Python attribute, right?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
herz
  • 35
  • 1
  • 4

2 Answers2

0

Im gonna answer the first question. Basically when you do just an import, python imports the entire file with all of its modules and functions. Like when you import math you can use math.ceil and other functions. However when you say from math import add you only get a specific module which is ceil like ceil(2.7). For further details read up here

Moon
  • 72
  • 1
  • 9
0
  1. Does the first import ("import os") mean that I'm only using a Module called "os"?

As the statement implies, you're importing the OS module, so you can use the functions in the os module in your python script.

So, now you can make os.function() statements in your script. The OS module is installed with Python by default. Here is info on the os module.

  1. Does the second import ("from flask import Flask") mean that I'm using the package "flask" and import the module "Flask"? If, e.g., there would be another import like "render_template", does that mean I'm using this module or is it a method from the module "Flask"?

This can be confusing since the function name and the import statement have the same name. You're only importing the function flask from the module Flask, not all the functions present in the Flask module. This can be done for multiple reasons. On is to simplify calling the function. Another could be to save system resources, since you're only

  1. os => means that I'm using the already imported module "os", right? path => means that I'm using an attribute from that module? abspath => means that I'm using a method within the "os" module called "abspath(value)"?

Exactly, read the docs for an explanation by the developers of the module.

  1. Filename

Here is an explanation of the filename usage in Python.

Kevin C
  • 4,851
  • 8
  • 30
  • 64