Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import functions directly from Python 3 modules

I have the following folder structure for a Python 3 project where vehicle.py is the main script and the folder stats is treated as a package containing several modules:

enter image description here

The cars module defines the following functions:

def neon():
    print('Neon')
    print('mpg = 32')


def mustang():
    print('Mustang')
    print('mpg = 27')

Using Python 3, I can access the functions in each module from within vehicle.py as follows:

import stats.cars as c

c.mustang()

However, I would like to access the functions defined in each module directly, but I receive an error when doing this:

import stats as st

st.mustang()
# AttributeError: 'module' object has no attribute 'mustang'

I also tried placing an __init__.py file in the stats folder with the following code:

from cars import *
from trucks import *

but I still receive an error:

import stats as st

st.mustang()
# ImportError: No module named 'cars'

I'm trying to use the same approach as NumPy such as:

import numpy as np

np.arange(10)
# prints array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

How can I create a package like NumPy in Python 3 to access functions directly in modules?

like image 944
wigging Avatar asked Dec 17 '15 18:12

wigging


People also ask

How do I import a function in Python 3?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

How do I import a function from a module?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.

How do you import all functions in Python?

To import all functions in a script, use * . This imports all the functions defined in script_in_cwd.py . If script_in_cwd.py has import statements, then * will also import those libraries, modules, and functions. For instance, if script_in_cwd.py had import numpy , then the above statement would also import numpy .

What are the three ways to import modules in Python?

So there's four different ways to import: Import the whole module using its original name: pycon import random. Import specific things from the module: pycon from random import choice, randint. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd.


1 Answers

Put an __init__.py file in the stats folder (as others have said), and put this in it:

from .cars import neon, mustang
from .trucks import truck_a, truck_b

Not so neat, but easier would be to use the * wildcard:

from .cars import *
from .trucks import *

This way, the __init__.py script does some importing for you, into its own namespace.

Now you can use functions/classes from the neon/mustang module directly after you import stats:

import stats as st
st.mustang()
like image 182
Def_Os Avatar answered Nov 03 '22 00:11

Def_Os