Questions tagged [python-import]

For questions about importing modules in Python

Importing modules with python:

  • import random

You can use functions from the random module by writing random. in front of the function, for example random.randint

  • import random as rand

The same as above but instead writing rand. before the function

  • from random import ...

imports specific functions, or * for all into your program, you can call the functions as if they were in the file without writing anything before them.

4828 questions
478
votes
20 answers

Use 'import module' or 'from module import'?

I've tried to find a comprehensive guide on whether it is best to use import module or from module import. I've just started with Python and I'm trying to start off with best practices in mind. Basically, I was hoping if anyone could share their…
Filip Dupanović
  • 28,429
  • 11
  • 76
  • 105
454
votes
12 answers

Automatically create requirements.txt

Sometimes I download the python source code from github and don't know how to install all the dependencies. If there is no requirements.txt file I have to create it by hands. The question is: Given the python source code directory is it possible to…
Igor Barinov
  • 12,534
  • 10
  • 26
  • 33
309
votes
6 answers

`from ... import` vs `import .`

I'm wondering if there's any difference between the code fragment from urllib import request and the fragment import urllib.request or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there…
wchargin
  • 14,264
  • 11
  • 61
  • 106
295
votes
19 answers

How to load all modules in a folder?

Could someone provide me with a good way of importing a whole directory of modules? I have a structure like this: /Foo bar.py spam.py eggs.py I tried just converting it to a package by adding __init__.py and doing from Foo import * but…
Evan Fosmark
  • 89,147
  • 33
  • 99
  • 116
262
votes
16 answers

Sibling package imports

I've tried reading through questions about sibling imports and even the package documentation, but I've yet to find an answer. With the following structure: ├── LICENSE.md ├── README.md ├── api │   ├── __init__.py │   ├── api.py │   └──…
zachwill
  • 3,294
  • 3
  • 16
  • 17
248
votes
13 answers

Relative imports - ModuleNotFoundError: No module named x

This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files: test.py config.py config.py has a few functions defined in it as well as a few variables. I've stripped it down to…
blitzmann
  • 5,071
  • 4
  • 20
  • 28
247
votes
8 answers

How to import a Python class that is in a directory above?

I want to inherit from a class in a file that lies in a directory above the current one. Is it possible to relatively import that file?
user129975
  • 2,835
  • 3
  • 16
  • 16
245
votes
7 answers

ModuleNotFoundError: What does it mean __main__ is not a package?

I am trying to run a module from the console. The structure of my directory is this: I am trying to run the module p_03_using_bisection_search.py, from the problem_set_02 directory using: $ python3 p_03_using_bisection_search.py The code inside…
lmiguelvargasf
  • 40,464
  • 32
  • 169
  • 172
209
votes
9 answers

Why is Python running my module when I import it, and how do I stop it?

I have a Python program I'm building that can be run in either of 2 ways: the first is to call "python main.py" which prompts the user for input in a friendly manner and then runs the user input through the program. The other way is to call "python…
Dasmowenator
  • 4,229
  • 4
  • 28
  • 42
205
votes
13 answers

How to check if a python module exists without importing it

I need to know if a python module exists, without importing it. Importing something that might not exist (not what I want): try: import eggs except ImportError: pass
yarbelk
  • 5,725
  • 5
  • 25
  • 34
196
votes
6 answers

In Python, what happens when you import inside of a function?

What are the pros and cons of importing a Python module and/or function inside of a function, with respect to efficiency of speed and of memory? Does it re-import every time the function is run, or perhaps just once at the beginning whether or not…
Tim McJilton
  • 5,634
  • 6
  • 22
  • 27
193
votes
29 answers

django import error - No module named core.management

Ok, I see plenty of these errors around. I have tried everything I know to do and have yet to figure this out. I am working on a development server running python 2.5 and Django 1.3. Django 1.3 was installed using python setup.py install after…
grantk
  • 3,698
  • 4
  • 25
  • 36
192
votes
10 answers

How to dynamically load a Python class

Given a string of a Python class, e.g. my_package.my_module.MyClass, what is the best possible way to load it? In other words I am looking for a equivalent Class.forName() in Java, function in Python. It needs to work on Google App…
pjesi
  • 3,421
  • 3
  • 18
  • 15
192
votes
6 answers

What's the correct way to sort Python `import x` and `from x import y` statements?

The python style guide suggests to group imports like this: Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports However, it does not mention anything…
ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
177
votes
2 answers

What does from __future__ import absolute_import actually do?

I have answered a question regarding absolute imports in Python, which I thought I understood based on reading the Python 2.5 changelog and accompanying PEP. However, upon installing Python 2.5 and attempting to craft an example of properly using…
Two-Bit Alchemist
  • 15,547
  • 5
  • 38
  • 74