Temporary Linux Python Working Direction

First Post:

Last Update:

Word Count:
556

Read Time:
3 min

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.


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
export PYTHONPATH=$(pwd)

Explanation:

  • export PYTHONPATH: This Linux command is used to set or modify the PYTHONPATH environment variable. Python checks the paths defined in this variable when searching for modules.
  • $(pwd): This is a command substitution; the pwd command outputs the absolute path of the current working directory. $(pwd) uses its output as an argument for the export command.

Example:

Assume your file structure is like this:

1
2
3
my_project/
├── main.py
└── my_module.py

my_module.py content:

1
2
def hello():
print("Hello from my_module!")

main.py content:

1
2
3
import my_module

my_module.hello()
  1. Enter the my_project directory:
    1
    cd my_project
  2. Set PYTHONPATH:
    1
    export PYTHONPATH=$(pwd)
  3. Run main.py:
    1
    python main.py
    You should see the output “Hello from my_module!“

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
2
3
4
5
6
7
8
9
10
11
12
import sys
import os

# Add the directory containing the current script to Python's search path
# os.path.dirname(__file__) gets the directory of the current script
# os.path.abspath() gets the absolute path
current_dir = os.path.abspath(os.path.dirname(__file__))
if current_dir not in sys.path:
sys.path.append(current_dir)

# Now you can import modules from the current directory
import your_module_name

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
2
3
my_project/
├── __init__.py
└── my_script.py

And you want to run my_script.py:

1
python -m my_project.my_script

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.