Temporary Linux Python Working Direction
Last Update:
Word Count:
Read Time:
Page View: loading...
How to Quickly Set Current Directory as Python’s Working Directory in Linux
To enable Python in a Linux environment to directly import
modules from the current directory, the most common and recommended approach is to modify the Python module search path. Below are several common methods, along with their pros and cons.
1. Temporarily Modifying the [$PYTHONPATH] Environment Variable (Recommended)
This is the most common and flexible method, as it only affects the current session or script execution.
In your terminal, navigate to the directory containing your Python modules, then run:
1 |
|
Explanation:
export PYTHONPATH
: This Linux command is used to set or modify thePYTHONPATH
environment variable. Python checks the paths defined in this variable when searching for modules.$(pwd)
: This is a command substitution; thepwd
command outputs the absolute path of the current working directory.$(pwd)
uses its output as an argument for theexport
command.
Example:
Assume your file structure is like this:
1 |
|
my_module.py
content:
1 |
|
main.py
content:
1 |
|
- Enter the
my_project
directory:1
cd my_project
- Set
PYTHONPATH
:1
export PYTHONPATH=$(pwd)
- Run
main.py
:You should see the output “Hello from my_module!“1
python main.py
Pros:
- Non-invasive: Only effective for the current terminal session or script, does not permanently alter system configuration.
- Flexible: Suitable for any scenario requiring temporary path additions.
Cons:
- Needs to be set again every time you open a new terminal session.
2. Adding Path via [sys.path.append()] in Python Script
You can also directly add the current directory to the module search path at the beginning of your Python script.
1 |
|
Pros:
- Self-contained: Handled automatically when the script runs, no external commands needed.
- Portable: The script can run in any environment without additional setup.
Cons:
- Requires adding this code to every script that needs this functionality.
- If modules are in subdirectories of the current directory, it requires more complex path handling.
3. Using the [-m] Option (for Executable Modules/Packages)
If your Python structure is an executable package, you can use the -m
option to run a module. This automatically handles module path issues.
For example, if your my_project
structure looks like this:
1 |
|
And you want to run my_script.py
:
1 |
|
Pros:
- Suitable for building executable packages.
- Python handles paths automatically.
Cons:
- Requires your code to be organized as a Python package (i.e., include an
__init__.py
file).
Summary
For most temporary development and testing scenarios, the first method (export PYTHONPATH=$(pwd)
) is the most concise and recommended. It allows you to run Python scripts directly in the current directory and import sibling modules without modifying the script itself.
If you need a more robust solution, such as building a large project, it’s typically recommended to use a Python package structure and manage module dependencies and paths via pip install -e .
(editable install) or by building a setup.py
file.