Libraries: Where are they? How do I integrate them into my project?


#1

https://www.codecademy.com/courses/python-beginner-en-zFPOx/0/2?curriculum_id=4f89dab3d788890003000096


This lesson says:

The first line imports the datetime library so that we can use it.

I looked up the definition of "library" as

a collection of programs and software packages made generally available, often loaded and stored on disk for immediate use.

My confusion as a total beginner is "Where did this library come from?" If I was using Python on my computer without using codeacademy, would I have had to go find the datetime library and somehow know where to put it and how to integrate it into my project? Or is it part of a standard Python install, just waiting on my hard drive to be imported and used?

I understand that for this particular assignment it just happens for me, but I don't learn specific things very well without also learning an overview of how the bigger picture works.

Thank you!


#2

The standard library is part of the Python distribution, yes. (Which is why it's called the standard library)
You can also download other code and import that. Python also ships with a program named pip which can search and download code from pypi (python package index)

In their simplest form, modules are just regular python files.

# my_module.py


def say_hi():
    print('hello world')

# main.py
import my_module


my_module.say_hi()  # prints 'hello world'

install something:

$ pip install --user flask
$ python
Python 3.6.0 (default, Jan 16 2017, 12:12:55)
[GCC 6.3.1 20170109] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
>>> # (imported successfully - no error)

#3

Thank you for the answer. In your example, there is a "$" prefix in the first two lines. What does that do? Is it Python-specific? (I am using Windows if it makes any difference in this case.) I am confused because elsewhere I saw someone write "Whenever you see the $ sign, simply ignore it and type in the rest of the line without modification." I'm a whole 2 days into computer languages if that gives you some context!


#4

No, it's.. quite universal.

There are two cli's in my pip example, the second is python itself, the first is called bash (which is also a language)
So.. you write what you want to happen. Same as when writing a program, except just one instruction at a time


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.