Questions tagged [python-import]
For questions about importing modules in Python
python-import
6,025
questions
2571
votes
41
answers
3.6m
views
Importing files from different folder
I have this folder structure:
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
How can I import a function from file.py, from within ...
1806
votes
38
answers
1.5m
views
How can I import a module dynamically given the full path?
How do I load a Python module given its full path?
Note that the file can be anywhere in the filesystem where the user has access rights.
See also: How to import a module given its name as string?
1794
votes
15
answers
658k
views
Relative imports for the billionth time
I've been here:
PEP 328 – Imports: Multi-Line and Absolute/Relative
Modules, Packages
Python packages: relative imports
Python relative import example code does not work
Relative imports in Python 2....
1759
votes
33
answers
1.9m
views
Relative imports in Python 3
I want to import a function from another file in the same directory.
Usually, one of the following works:
from .mymodule import myfunction
from mymodule import myfunction
...but the other one gives ...
1347
votes
28
answers
2.1m
views
Automatically create file 'requirements.txt'
Sometimes I download the Python source code from GitHub and don't know how to install all the dependencies. If there isn't any requirements.txt file I have to create it by hand.
Given the Python ...
1273
votes
23
answers
3.0m
views
How do I import other Python files?
How do I import files in Python? I want to import:
a file (e.g. file.py)
a folder
a file dynamically at runtime, based on user input
one specific part of a file (e.g. a single function)
1118
votes
22
answers
945k
views
How do I unload (reload) a Python module?
I have a long-running Python server and would like to be able to upgrade a service without restarting the server. What's the best way do do this?
if foo.py has changed:
unimport foo <-- How ...
1116
votes
33
answers
1.5m
views
Importing modules from parent folder
I am running Python 2.5.
This is my folder tree:
ptdraft/
nib.py
simulations/
life/
life.py
(I also have __init__.py in each folder, omitted here for readability)
How do I import the ...
955
votes
37
answers
2.8m
views
ImportError: No module named requests
I tried importing requests:
import requests
But I get an error:
ImportError: No module named requests
949
votes
20
answers
1.7m
views
How to import the class within the same directory or sub directory?
I have a directory that stores all the .py files.
bin/
main.py
user.py # where class User resides
dir.py # where class Dir resides
I want to use classes from user.py and dir.py in main.py.
...
842
votes
23
answers
664k
views
How to fix "Attempted relative import in non-package" even with __init__.py
I'm trying to follow PEP 328, with the following directory structure:
pkg/
__init__.py
components/
core.py
__init__.py
tests/
core_test.py
__init__.py
In core_test.py I have ...
803
votes
22
answers
947k
views
Import a module from a relative path
How do I import a Python module given its relative path?
For example, if dirFoo contains Foo.py and dirBar, and dirBar contains Bar.py, how do I import Bar.py into Foo.py?
Here's a visual ...
756
votes
16
answers
1.1m
views
What can I do about "ImportError: Cannot import name X" or "AttributeError: ... (most likely due to a circular import)"?
I have some code spread across multiple files that try to import from each other, as follows:
main.py:
from entity import Ent
entity.py:
from physics import Physics
class Ent:
...
physics.py:
...
707
votes
10
answers
484k
views
How can I import a module dynamically given its name as string?
I'm writing a Python application that takes a command as an argument, for example:
$ python myapp.py command1
I want the application to be extensible, that is, to be able to add new modules that ...
665
votes
13
answers
809k
views
Import a file from a subdirectory?
I have a file called tester.py, located on /project.
/project has a subdirectory called lib, with a file called BoxTime.py:
/project/tester.py
/project/lib/BoxTime.py
I want to import BoxTime from ...
636
votes
38
answers
2.5m
views
Python error "ImportError: No module named"
Python is installed in a local directory.
My directory tree looks like this:
(local directory)/site-packages/toolkit/interface.py
My code is in here:
(local directory)/site-packages/toolkit/...
622
votes
16
answers
423k
views
How can I do relative imports in Python?
Imagine this directory structure:
app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
I'm coding mod1, and I need to import something from mod2. How ...
619
votes
23
answers
272k
views
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.
...
559
votes
16
answers
417k
views
What happens when using mutual or circular (cyclic) imports?
In Python, what happens when two modules attempt to import each other? More generally, what happens if multiple modules attempt to import in a cycle?
See also What can I do about "ImportError: ...
448
votes
27
answers
1.1m
views
ImportError: No module named 'Tkinter' [duplicate]
For some reason, I can't use the Tkinter (or tkinter, on Python 3) module.
After running the following command in the python shell:
import Tkinter
or this, in Python 3:
import tkinter
I got this ...
438
votes
19
answers
1.1m
views
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 ...
388
votes
12
answers
240k
views
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
│ ├...
380
votes
6
answers
597k
views
`from ... import` vs `import .` [duplicate]
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 ...
371
votes
22
answers
450k
views
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 ...
315
votes
11
answers
406k
views
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?
285
votes
14
answers
290k
views
How to check if a Python module exists without importing it
How can I know if a Python module exists, without importing it?
Importing something that might not exist (not what I want) results in:
try:
import eggs
except ImportError:
pass
282
votes
12
answers
288k
views
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 ...
277
votes
6
answers
198k
views
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 ...
276
votes
6
answers
151k
views
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/...
263
votes
7
answers
231k
views
Importing module from string variable using "__import__" gives different results than a normal import statement
I'm working on a documentation (personal) for nested matplotlib (MPL) library, which differs from MPL own provided, by interested submodule packages. I'm writing Python script which I hope will ...
259
votes
6
answers
343k
views
ModuleNotFoundError: What does it mean __main__ is not a package? [duplicate]
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 ...
252
votes
29
answers
451k
views
No module named _sqlite3
I am trying to run a Django app on my VPS running Debian 5. When I run a demo app, it comes back with this error:
File "/usr/local/lib/python2.5/site-packages/django/utils/importlib.py", line 35, ...
247
votes
16
answers
225k
views
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....
221
votes
11
answers
121k
views
How to mock an import
Module A includes import B at its top. However under test conditions I'd like to mock B in A (mock A.B) and completely refrain from importing B.
In fact, B isn't installed in the test environment on ...
209
votes
2
answers
142k
views
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 ...
207
votes
34
answers
385k
views
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 ...
200
votes
12
answers
83k
views
Why is "import *" bad?
It is recommended to not to use import * in Python.
Can anyone please share the reason for that, so that I can avoid it doing next time?
197
votes
3
answers
146k
views
Conditional import of modules in Python [duplicate]
In my program I want to import simplejson or json based on OS being Linux or Windows. I take the OS name as input from the user. Now, is it correct to do it with a condition like this?
osys = ...
194
votes
6
answers
160k
views
Can you define aliases for imported modules in Python?
In Python, is it possible to define an alias for an imported module?
For instance:
import a_ridiculously_long_module_name
...so that is has an alias of 'short_name'.
193
votes
6
answers
518k
views
adding directory to sys.path /PYTHONPATH
I am trying to import a module from a particular directory.
The problem is that if I use sys.path.append(mod_directory) to append the path and then open the python interpreter, the directory ...
185
votes
10
answers
240k
views
Visibility of global variables in imported modules [duplicate]
I've run into a bit of a wall importing modules in a Python script. I'll do my best to describe the error, why I run into it, and why I'm tying this particular approach to solve my problem (which I ...
181
votes
21
answers
347k
views
Check if Python Package is installed
What's a good way to check if a package is installed while within a Python script? I know it's easy from the interpreter, but I need to do it within a script.
I guess I could check if there's a ...
166
votes
6
answers
133k
views
Should I use `import os.path` or `import os`?
According to the official documentation, os.path is a module. Thus, what is the preferred way of importing it?
# Should I always import it explicitly?
import os.path
Or...
# Is importing os enough?
...
165
votes
4
answers
296k
views
Attempted relative import with no known parent package [duplicate]
from ..box_utils import decode, nms
This line is giving error
ImportError: attempted relative import with no known parent package
What is this error and how to resolve this error?
160
votes
5
answers
155k
views
How can I check if a module has been imported?
How do I check if I imported a module somewhere in the code?
if not has_imported("somemodule"):
print('you have not imported somemodule')
The reason that I would like to check if I ...
156
votes
12
answers
333k
views
Python: Best way to add to sys.path relative to the current running script
I have a directory full of scripts (let's say project/bin). I also have a library located in project/lib and want the scripts to automatically load it. This is what I normally use at the top of each ...
151
votes
9
answers
176k
views
ImportError: libSM.so.6: cannot open shared object file: No such file or directory
When trying to import OpenCV, using import cv2 I get the following error:
/usr/local/lib/python2.7/dist-packages/cv2/__init__.py in <module>()
7
8 # make IDE's (PyCharm) ...
147
votes
6
answers
261k
views
Reading a file using a relative path in a Python project
Say I have a Python project that is structured as follows:
project
/data
test.csv
/package
__init__.py
module.py
main.py
__init__.py:
from .module import test
...
146
votes
6
answers
72k
views
When to use os.name, sys.platform, or platform.system?
As far as I know, Python has 3 ways of finding out what operating system is running on:
os.name
sys.platform
platform.system()
Knowing this information is often useful in conditional imports, or ...
142
votes
9
answers
69k
views
Reload component Y imported with 'from X import Y'
In Python, once I have imported a module X in an interpreter session using import X, and the module changes on the outside, I can reload the module with reload(X). The changes then become available in ...