What is the correct way to import functions from other packages in python?

1

If I'm going to create a python package, I'll define a setup.py file that has something like this:

from setuptools import setup

setup(name='funniest',
      version='0.1',
      description='The funniest joke in the world',
      url='http://github.com/storborg/funniest',
      author='Flying Circus',
      author_email='[email protected]',
      license='MIT',
      packages=['funniest'],
      install_requires=[
          'markdown',
      ],
      zip_safe=False)

Is there a rule of good practice on how to refer to packages declared as dependencies in the code?

For example in a file in my package, I should use import markdown in the beginning. Or, when using a function from the package markdown I should use markdown.x() .

I will explain the motivation of the question. In% with%% of% is R however this function has side-effects (it loads all functions for the user's global environment) and should not be used within packages. In import I do not know how it is, so I'm asked. (Read this before voting to close)

    
asked by anonymous 23.09.2016 / 21:07

1 answer

2

This case is related to the normal use of Python packages even. As your package has dependencies they will be installed with a normal package, so they will have their way of importing normal like you would have developed any program.

Here are the standard Python import cases. If you are going to use only some function of a particular package, for example, the package math will only use sqrt so it only imports it, not everything.

from math import sqrt

But it has the form where you import the whole package import math and throughout the script you use math.funcao to specify what you want.

In your case for internal functions of packages it is recommended to import only them.

from pacote import funcao
    
24.09.2016 / 22:36