Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing packages in Python

I am probably missing something obvious but anyway:

When you import a package like os in python, you can use any submodules/subpackages off the bat. For example this works:

>>> import os >>> os.path.abspath(...) 

However I have my own package which is structured as follows:

FooPackage/   __init__.py   foo.py 

and here the same logic does not work:

>>> import FooPackage >>> FooPackage.foo AttributeError: 'module' object has no attribute 'foo' 

What am I doing wrong?

like image 348
miki725 Avatar asked Jan 28 '12 20:01

miki725


People also ask

Can you import packages in Python?

We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it.

How do you create and import a package in Python?

Follow the below steps to create a package in PythonCreate a directory and include a __init__.py file in it to tell Python that the current directory is a package. Include other sub-packages or files you want. Next, access them with the valid import statements.

How do you add a package to Python?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

What does it mean to import a package in Python?

Importing refers to allowing a Python file or a Python module to access the script from another Python file or module. You can only use functions and properties your program can access. For instance, if you want to use mathematical functionalities, you must import the math package first.


2 Answers

When you import FooPackage, Python searches the directories on PYTHONPATH until it finds a file called FooPackage.py or a directory called FooPackage containing a file called __init__.py. However, having found the package directory, it does not then scan that directory and automatically import all .py files.

There are two reasons for this behaviour. The first is that importing a module executes Python code which may take time, memory, or have side effects. So you might want to import a.b.c.d without necessarily importing all of a huge package a. It's up to the package designer to decide whether a's __init__.py explicitly imports its modules and subpackages so that they are always available, or whether or leaves the client program the ability to pick and choose what is loaded.

The second is a bit more subtle, and also a showstopper. Without an explicit import statement (either in FooPackage/__init__.py or in the client program), Python doesn't necessarily know what name it should import foo.py as. On a case insensitive file system (such as used in Windows), this could represent a module named foo, Foo, FOO, fOo, foO, FoO, FOo, or fOO. All of these are valid, distinct Python identifiers, so Python just doesn't have enough information from the file alone to know what you mean. Therefore, in order to behave consistently on all systems, it requires an explicit import statement somewhere to clarify the name, even on file systems where full case information is available.

like image 105
Ben Avatar answered Oct 21 '22 13:10

Ben


You need to import the submodule:

import FooPackage.foo 

What you're doing is looking for foo in FooPackage/__init__.py. You could solve it by putting import FooPackage.foo as foo (or from . import foo) in FooPackage/__init__.py, then Python will be able to find foo there. But I recommend using my first suggestion.

like image 26
Rob Wouters Avatar answered Oct 21 '22 13:10

Rob Wouters