Using classes, functions or other objects from an installed package

Question:

I successfully installed this package using pip:

pip install functional-dependencies

Output:

Collecting functional-dependencies
  Downloading functional_dependencies-1.3.0-py2.py3-none-any.whl (33 kB)
Installing collected packages: functional-dependencies
Successfully installed functional-dependencies-1.3.0

which should provide tools for functional dependencies (FDs).

However, trying to get FDs using the commands as per the documentation results in an error:

Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> fd1 = FD("CustomerID", "DateOfBirth")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'FD' is not defined

How can I access the classes, functions and objects from the installed package?

Asked By: Sami

||

Answers:

Although this is a very basic question, just because the official documentation does not include full working examples, I will provide an answer here.

You have already installed the module functional-dependencies. To use it, you’ll need to know the module name to use to import. This is written in the first line of the introduction:

The module functional_dependencies defines the classes FD, FDSet, and RelSchema to represent a single functional dependency (FD), a set of FDs, and a relation schema, respectively.

So, you’ll start by doing:

import functional_dependencies

or

from functional_dependencies import *

The latter is generally not recommended because of namespace pollution, but since you probably want to copy-paste examples from the documentation, this will be easier.

Now you can do:

from functional_dependencies import *

fd1 = FD("CustomerID", "DateOfBirth")
fd1.isrminimal()

Otherwise you would have to include the module name to access the classes:

import functional_dependencies

fd1 = functional_dependencies.FD("CustomerID", "DateOfBirth")
fd1.isrminimal()

Now, for the rest of your journey, reading the documentation should be helpful. Other tricks that might be useful is typing help(functional_dependencies) in the Python interpreter. It will print all functions and classes that are defined in the module, including the available documentation. You can also do this on an object: help(fd1) would print help about the object. You can also execute dir(fd1) to see which attributes the object has, which includes all its methods.

Answered By: wovano
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.