custom module import - ibrahimrifats/Back-End-development GitHub Wiki

## Adding a Directory to Python Path

In Python, the `sys.path` list contains directories where Python looks for modules when you import them. You can add custom directories to this path to make your own modules accessible. Here's how you can do it:

1. First, import the `sys` module at the beginning of your script:
   ```python
   import sys
  1. Use the sys.path.insert() method to insert the directory you want to add. The second argument of insert() specifies the position in the list where the new path should be added. In this example, we're adding a directory called 'G:\temp practice\Workplace' at position 1:

    sys.path.insert(1, r'G:\temp practice\Workplace')
    
  2. After adding the path, you can now import modules from that directory. For instance, if there's a module called trial in the specified directory, you can import it like this:

    import trial
    
  3. Now you can use the imported module as you would with any other module:

    print(trial)
    

By adding a directory to the Python path, you can organize your code into separate modules and make them accessible across different scripts. Keep in mind that modifying sys.path affects the current session only. If you want the path to be permanently added, consider setting environment variables or modifying configuration files.

#   a directory: Workplace/trial.py 

#  but if I wanna use it another code as a module then we can follow this:

# this directory of you running program, LetsTrial/test.py

import sys 

sys.path.insert(1, r'G:\temp practice\Workplace')

import trial
print(trial)