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.

5867 questions
2448
votes
40 answers

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 some_file.py? I tried: from application.app.folder.file…
Ivan
  • 28,999
  • 6
  • 24
  • 21
1747
votes
35 answers

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?
derfred
  • 18,881
  • 3
  • 23
  • 25
1609
votes
31 answers

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 me one of these errors: ImportError: attempted…
John Smith Optional
  • 22,259
  • 12
  • 43
  • 61
1211
votes
23 answers

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)
Tamer
  • 12,137
  • 3
  • 16
  • 4
1168
votes
26 answers

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 source code directory, is it possible to create…
Igor Barinov
  • 21,820
  • 10
  • 28
  • 33
1081
votes
22 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
  • 297,451
  • 125
  • 333
  • 465
1069
votes
32 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
  • 84,019
  • 84
  • 236
  • 374
917
votes
20 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
  • 61,507
  • 53
  • 142
  • 183
909
votes
34 answers

ImportError: No module named requests

I tried importing requests: import requests But I get an error: ImportError: No module named requests
user2476540
  • 9,265
  • 4
  • 15
  • 9
832
votes
21 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
  • 11,467
  • 7
  • 43
  • 61
802
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,977
  • 7
  • 28
  • 27
735
votes
17 answers

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: from entity import Ent class Physics: ... I…
jsells
  • 7,359
  • 3
  • 14
  • 3
678
votes
10 answers

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 implement new commands without having to change the…
Kamil Kisiel
  • 19,723
  • 11
  • 46
  • 56
632
votes
13 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
  • 128,757
  • 147
  • 397
  • 562
615
votes
38 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
  • 19,928
  • 23
  • 65
  • 73
1
2 3
99 100