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
1798
votes
31 answers

Importing files from different folder

I have the following folder structure. application ├── app │   └── folder │   └── file.py └── app2 └── some_folder └── some_file.py I want to import some functions from file.py in some_file.py. I've tried from…
Ivan
  • 21,917
  • 6
  • 22
  • 21
1352
votes
32 answers

How to import a module given the full path?

How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.
derfred
  • 14,815
  • 3
  • 21
  • 25
1043
votes
18 answers

Relative imports in Python 3

I want to import a function from another file in the same directory. Sometimes it works for me with from .mymodule import myfunction but sometimes I get a: SystemError: Parent module '' not loaded, cannot perform relative import Sometimes it works…
John Smith Optional
  • 15,639
  • 10
  • 36
  • 54
951
votes
21 answers

How to import other Python files?

How do I import other files in Python? How exactly can I import a specific python file like import file.py? How can I import a folder instead of a specific file? I want to load a Python file dynamically at runtime, based on user input. I want to…
Tamer
  • 9,529
  • 3
  • 14
  • 4
905
votes
21 answers

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 do I do this? import foo myfoo = foo.Foo()
Mark Harrison
  • 267,774
  • 112
  • 308
  • 434
792
votes
15 answers

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. How can I import these Python classes into…
Bin Chen
  • 54,865
  • 51
  • 136
  • 180
777
votes
22 answers

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 representation: dirFoo\ Foo.py dirBar\ …
Jude Allred
  • 10,577
  • 7
  • 25
  • 27
772
votes
20 answers

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 the following import statement from…
skytreader
  • 10,673
  • 5
  • 39
  • 57
766
votes
24 answers

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 nib module from inside the life module? I am hoping…
Ram Rachum
  • 71,442
  • 73
  • 210
  • 338
682
votes
27 answers

ImportError: No module named requests

Whenever I try to import requests, I get an error saying No module Named requests. import requests The error I get: File "ex2.py", line 1, in import requests ImportError: No module named requests
user2476540
  • 6,965
  • 4
  • 11
  • 9
616
votes
16 answers

ImportError: Cannot import name X

I have four different files named: main.py, vector.py, entity.py and physics.py. I will not post all the code, just the imports, because I think that's where the error is (If you want, I can post more). main.py: import time from entity import…
jsells
  • 6,169
  • 3
  • 12
  • 3
583
votes
11 answers

How to import a module given its name as string?

I'm writing a Python application that takes as 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 implement new commands without having to change…
Kamil Kisiel
  • 17,767
  • 10
  • 44
  • 54
552
votes
15 answers

How to 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 should I do it? I tried from ..sub2 import mod2…
Joril
  • 17,682
  • 13
  • 64
  • 84
524
votes
31 answers

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/examples/mountain.py To run the example, I write python…
Eduardo
  • 17,165
  • 20
  • 59
  • 71
520
votes
11 answers

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 tester. I have tried this: import…
Adam Matan
  • 107,447
  • 124
  • 346
  • 512
1
2 3
99 100