Same answer as @jil however, that answer is specific to some historical version of Python.

In modern Python (3.x):

exec(open('filename').read())

replaces execfile('filename') from 2.x

It can be used with a context manager, as mentioned by MartinUeding, to ensure the file closes:

with open('filename') as f:
    exec(f.read())
Answer from Hack-R on Stack Overflow
🌐
Python
python.org › downloads › source
Python Source Releases for Source release
Python>>> Downloads>>> Source code · Latest Python 3 Release - Python 3.15.2 · Python 3.11.18 - Oct. 25, 2026 · Download Gzipped source tarball · Download XZ compressed source tarball · Python 3.13.18 - Oct. 25, 2026 · Download Gzipped source tarball ·
Discussions

Sourcepy: source python files straight from the shell
Wait-- can you run python function from the commandline? How well does it work, how complex an the functions be? otherwise, looks cool More on reddit.com
🌐 r/Python
3
15
March 29, 2022
python - Get location of the .py source file - Stack Overflow
Say I have a python file in directory e like this: /a/b/c/d/e/file.py Under directory e I have a few folders I want to access but if file.py is executed from anywhere else rather than from folder... More on stackoverflow.com
🌐 stackoverflow.com
can someone explain what is the python file like Python shell and window … ect , im confused
Terminal / shell. Generic terms for text interface applications, like PowerShell, command prompt, bash, etc. You enter commands for that system to run, the system being either your operating system or some other framework. Python file - typically a .py extension. It's a text document that contains multiple lines of python code, and may call to external files or import additional python files/modules. Python terminal - python program running in an interactive mode. You can type python code (one line at a time) and it will run. Typical terminal and python terminal may support pasting multiple lines in at a time, but whatever is pasted will run after each new line character, so be careful. Python window - nothing more than probably just the python terminal window that stays open after you run the python executable in interactive mode. Essentially interchangeable with python terminal. More on reddit.com
🌐 r/learnpython
5
0
November 25, 2024
Is there a way to get the source code of a module?
Modules have an attribute __file__, which contains a file path. >> import collections >> print(collections.__file__) C:\Users\user\anaconda3\lib\collections\__init__.py More on reddit.com
🌐 r/learnpython
6
2
March 29, 2021
🌐
File Formats Wiki
fileformats.fandom.com › wiki › Python_source_file
Python source file | File Formats Wiki | Fandom
July 29, 2026 - Python source files are files that contain Python source code. As Python can be used as a scripting language, Python source files can be considered as scripts. PYW files are invoked on pythonw.exe instead of python.exe in order to prevent a ...
🌐
gtoolkit book
book.gtoolkit.com › working-with-python-source-files-1f5nlu7ul60puw4d8lvtsrkij
Working with Python source files
When working with a larger source tree you can still copy over files, but that is not the optimal solution. The better solution is to use the source tree as it is, either by extending Python's code load path, or by creating a symbolic link in the working directory.
🌐
Rune Hub
rune.codes › home › tutorials › python › python source files and scripts
Python Source Files and Scripts: .py Files Explained | Rune Hub
July 5, 2026 - This file is called a source file, and it contains Python statements that the interpreter reads and executes. Up until now, you have probably been typing code into the interactive interpreter or running single files with a few lines.
🌐
Python
docs.python.org › 3 › library › py_compile.html
py_compile — Compile Python source files — Python 3.15.2 documentation
Source code: Lib/py_compile.py The py_compile module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a scrip...
🌐
Reddit
reddit.com › r/python › sourcepy: source python files straight from the shell
r/Python on Reddit: Sourcepy: source python files straight from the shell
March 29, 2022 -

Hey folks, sharing a project I built just for fun, Sourcepy: https://github.com/dchevell/sourcepy

I was writing some increasingly messy shell functions and thought it would be nice if I could just write Python functions/variables/etc. instead and source them from the shell. So … that's what this is. It uses a mix of importlib and inspect machinery to "source" files (in reality, generating shell stubs that hook back into a bridging interface, and aliasing `source` so it works just like you’d expect).

Along the way I ended up deep diving into type annotations and turning plain python functions into dynamic command line apps that can coerce to native values, handle stdin, work with a large variety of types & objects and do a number of other interesting things - all without your code having to know a thing about Sourcepy. You write plain old python functions, add typehints to the parameters for the best results, and in most cases it should "just work".*

This is still a work in progress - its use case is largely for folks like me who like excuses to mess around in Python and customise their environment and toolset to an unnecessary degree.

Try it out, share any suggestions (or bugs, or criticism). I've poked around looking for similar projects and the only similar thing I could find required running a daemon which wasn't an approach I liked, so hopefully you'll find some novelty here.

*not a guarantee

Top answer
1 of 2
2
Wait-- can you run python function from the commandline? How well does it work, how complex an the functions be? otherwise, looks cool
2 of 2
1
Here's an example (mostly copied from the README): a basic grep function I wrote in Python. This is the entire thing - it accepts two parameters, a regex pattern (annotated with the re.Pattern type hint) and a list of one or more file-like objects (annotated with typing.TextIO). It's pretty normal python code, that aside from being properly type hinted has nothing to with Sourcepy and no knowledge of it: # pygrep.py from re import Pattern from typing import TextIO def pygrep(pattern: Pattern, grepdata: list[TextIO]): """ A minimal grep implementation in Python """ for file in grepdata: prefix = f'{file.name}:' if len(grepdata) > 1 else '' for line in file: if pattern.search(line): yield prefix + line Sourcepy can take that and source it as-is: $ source pygrep.py $ pygrep "implementation" pygrep.py A minimal grep implementation in Python And that's it, your Python function is available inside the shell. It will automatically generate help text through argparse: $ pygrep --help usage: pygrep [-h] [-p Pattern] [-g [file/stdin ...]] A minimal grep implementation in Python options: -h, --help show this help message and exit positional or keyword args: pattern (-p, --pattern) Pattern (required) grepdata (-g, --grepdata) [file/stdin ...] (required) It handles stdin and plays well with shell semantics: $ echo "one\ntwo\nthree" | pygrep --pattern "o" one two $ MYVAR=$(echo $RANDOM | pygrep "\d") $ echo $MYVAR 26636 It's even smart enough to redirect output to stderr, and only print the actual returned/yielded values to stdout: $ MYVAR=$(pygrep "I hope errors go to stderr" thisfiledoesnotexist) usage: pygrep [-h] [-p Pattern] [-g [file/stdin ...]] pygrep: error: argument grepdata: no such file or directory: thisfiledoesnotexist $ echo $MYVAR $ There's a whole lot more it can do, but ultimately the idea is that you shouldn't need to think about it - you should be able to write python functions and just "use" them, the same way you would if you were writing bash functions for your local env.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › what-is-the-difference-between-py-and-pyc-files
What Is The Difference Between .Py And .Pyc Files? - GeeksforGeeks
July 23, 2025 - A .py file is a plain text file that contains Python source code. This is the file you write your code in when you are developing a Python program.
🌐
ITNEXT
itnext.io › python-files-must-end-with-py-right-guess-again-50068377b5a6
Python files must end with .py, right? Guess again! | by Chris Karvouniaris | ITNEXT
April 1, 2024 - .py: This is the most common Python file extension. Python source code files use the .py extension.
🌐
FileFormat.com
docs.fileformat.com › programming › py
PY File Format
October 11, 2019 - The Files with the .py extension contain the Python source code. The Python language has become very famous language now a days. It can be used for system scripting, web and software development and mathematics. Python supports cross-platform compatibility; means that the developed applications ...
🌐
Python Packaging
packaging.python.org › specifications › source-distribution-format
Source distribution format - Python Packaging User Guide
A source tree is a collection of files and directories – like a version control system checkout – which contains a pyproject.toml file that can be used to build a source distribution from the contained files and directories.
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-source-a-python-file-from-another-python-file
How can I source a Python file from another Python file?
April 17, 2025 - The import statement is the most common way to include external Python files or modules in your script.
Top answer
1 of 5
3
Terminal / shell. Generic terms for text interface applications, like PowerShell, command prompt, bash, etc. You enter commands for that system to run, the system being either your operating system or some other framework. Python file - typically a .py extension. It's a text document that contains multiple lines of python code, and may call to external files or import additional python files/modules. Python terminal - python program running in an interactive mode. You can type python code (one line at a time) and it will run. Typical terminal and python terminal may support pasting multiple lines in at a time, but whatever is pasted will run after each new line character, so be careful. Python window - nothing more than probably just the python terminal window that stays open after you run the python executable in interactive mode. Essentially interchangeable with python terminal.
2 of 5
2
A Python file is just a text file, with code in it. It normally ends in .py so that your computer knows it contains Python code. Most beginner Python files contain statements that 'print' output. This happens in something called the terminal. When you run your Python file, anything you 'print' will appear as text in the terminal. Some Python files contain statements that let you 'input' text also, and this is also done in the terminal. 'Terminal,' 'Shell,' 'Console' and 'Command line' are all similar but slightly different terms, that it's East to get confused by. But they are all basically referring to that place where Python prints out commands and lets you input them, you can worry about the differences later.
🌐
The Coding Forums
thecodingforums.com › archive › archive › python
equivalent of source <file> in python? | Python | Coding Forums
August 24, 2010 - Click to expand... Rather use execfile here. Though you'd probably rather want to write a Python module and use it as such (you know, import).