You import a dictionary from another file by creating a .py file that contains the dict, then using a plain import or from ... import ... statement in the file that needs it. Python treats every .py file as a module, and any variable defined at the top level of a module — a dict included — becomes an attribute you can import and reuse. The only thing you need first is a working Python 3 install and both files in a location where Python can see them.
What You'll Need
- Python 3 installed and available on your command line (check with
python --versionorpython3 --version). - Two files in the same folder to start with: one that defines the dictionary and one that imports it.
- A text editor or IDE. Any will do — IDLE, VS Code, Notepad++, or even a terminal editor.
- Basic familiarity with running a script from the command line, or the ability to press Run in your editor.
Step-by-Step
-
Create the file that holds the dictionary. Make a new file called
settings.pyin your project folder. The filename becomes the module name, so keep it short, lowercase, and free of spaces or dashes. Underscores are fine. -
Define the dict at the top level of that file. Do not put it inside a function or class, or you will not be able to import it directly. Write something like:
config = {"host": "localhost", "port": 8080, "debug": True}
You can define several dicts in the same file; each one is importable by its variable name. -
Create the file that needs the dictionary. Name it
main.pyand place it in the same folder assettings.py. Keeping them side by side is the simplest layout and avoids path problems later. -
Import the module, then reference the dict through it. At the top of
main.pywriteimport settings. Then usesettings.configwherever you need the dictionary. This style keeps the origin of every value obvious in your code and avoids name clashes when two modules define similarly named dicts. -
Or import the dict directly. Write
from settings import configand then useconfigon its own. This is shorter and reads well when you need just one object. Usefrom settings import config, other_dictfor several at once. -
Run the script and check the output. From the project folder run
python main.py(orpython3 main.py). Print a value such asprint(config["port"])to confirm the import worked. Expect8080in this example. -
Copy the dictionary if you plan to modify it. The imported object is shared, not duplicated. If your code changes
config["port"], the change is visible to every other module that imported the same dict during that run. To edit safely, take a copy first:import copythenmy_config = copy.deepcopy(config)for nested dicts, orconfig.copy()for a flat one. -
Import from a subfolder if your project grows. Put the file in a package folder such as
myapp/, add an empty__init__.pyto that folder if you are using an older-style layout, and import withfrom myapp.settings import configorfrom .settings import configfrom another module inside the same package. Run such code as a module (python -m myapp.main) so the package root is on the path.
Common Mistakes
Naming the file after a standard library module. A file called json.py, random.py, types.py or email.py will shadow the real module and break other imports in confusing ways. Pick a name that describes your data, like app_config.py or defaults.py.
Importing a dict declared inside a function. A variable created inside a function is local and vanishes when the function returns. Import only works on names at module level, so move the dict to the top of the file.
Expecting changes to persist. Editing a dict at runtime does not rewrite the source file. Each run re-reads the file, so nothing you change in memory survives to the next run. If you need persistence, write to JSON or another file format instead.
Running the script from the wrong folder. Python adds the directory of the script you run to its import path, not the folder you happen to be sitting in. Launching from elsewhere can produce ModuleNotFoundError even though the file clearly exists.
Using from module import *. It hides where names come from and can silently overwrite your own variables. Import specific names instead.
Tips
Keep data-only modules free of side effects. A configuration file should define dicts and little else, so importing it never starts a server or opens a connection as an unexpected side effect. If you need an entry point, guard code with if __name__ == "__main__":.
Treat imported dicts as read-only by convention. When several modules share one config, mutating it creates bugs that are hard to trace, because the value changes depending on which import ran first.
Prefer JSON for data that changes or that other languages must read. A Python dict file is ideal for constants and defaults; a JSON file is better for user-editable settings, loaded with json.load().
Use relative imports inside a package. They survive folder renames better than absolute imports tied to the package name, and they make the module hierarchy explicit.
Keep import lines at the top of the file. Python style guides ask for this, and it makes dependencies visible at a glance rather than buried mid-file.
Troubleshooting
ModuleNotFoundError: No module named 'settings'. The file is not where Python is looking. Confirm the filename ends in .py and not .py.txt, that it sits in the same folder as the script you run, and that you run the command from that folder. On Windows, hidden file extensions are a frequent culprit because Notepad can save settings.py.txt without showing the extra suffix.
ImportError: cannot import name 'config'. The module was found but the dict name is wrong or misspelled, or it is nested inside a function or class. Check the exact spelling and capitalisation, then confirm the assignment sits at zero indentation.
Changing the file has no effect. Python caches compiled modules in a __pycache__ folder. It usually invalidates this automatically, but if you are in an interactive session or a long-running notebook kernel, reload explicitly with import importlib; importlib.reload(settings), or restart the session.
Values look stale or wrong. Another module may be modifying the shared dict. Search the project for assignments to the dict's keys, or take a deep copy before editing.
Name clash after a star import. A wildcard import can overwrite a variable you defined locally. Replace it with an explicit import of just the dict you need.
Frequently Asked Questions
Can I import a dict from a file in a different folder?
Yes. Either put that folder on the import path with sys.path.append(), or preferably place the file in a proper package and import it with a dotted path such as from myapp.settings import config, running the script as a module so the package root is found.
Does importing a dict copy it?
No. Both modules point at the same object in memory. Use copy.deepcopy() if you need an independent version you can change without affecting others.
Can I import a dict defined inside an if block?
Yes, as long as the condition was true at import time — the name exists at module level once the block runs. If the condition is false, the name never exists and the import fails. Put the dict at the top level to avoid depending on runtime conditions.
What is the difference between import settings and from settings import config?
The first imports the module and you reach the dict via settings.config. The second binds the dict itself to the local name config. Both import the whole module under the hood; only the namespace differs.
Should I use a dict file or a JSON file for configuration?
A dict file suits code that ships with fixed defaults. JSON suits settings a user or another program edits, because it is plain data with no Python code and can be read by tools outside Python.
Why does my dict import work in the editor but fail in the terminal?
The editor usually launches the script from the project root, while your terminal may be in a different folder. Change into the project folder first, or run the script with its full path, so the import path matches.