Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Module Imports by Avoiding Modification of sys.path #122

Closed
2 tasks done
shashank-iitbhu opened this issue Mar 24, 2024 · 7 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@shashank-iitbhu
Copy link
Contributor

Terms

Behavior

Description

Currently, in several scripts within the Scribe-Data project, we are modifying sys.path to include the src/scribe_data directory to ensure that the scribe_data module can be imported correctly. For example,

PATH_TO_SCRIBE_ORG = os.path.dirname(sys.path[0]).split("Scribe-Data")[0]
PATH_TO_SCRIBE_DATA_SRC = f"{PATH_TO_SCRIBE_ORG}Scribe-Data/src"
sys.path.insert(0, PATH_TO_SCRIBE_DATA_SRC)

This can lead to potential issues with import resolution and is generally not considered a best practice.

Suggested Changes

  • Remove such modification of sys.path.
  • Directly use from scribe_data import module_name
  • Encouraging developers to install the package in editable mode during development by using the command
    pip install -e . from the project root directory. This allows for testing changes to the code without needing to reinstall the package.
  • Python's import system uses the sys.path list to determine where to look for modules to import. After we have built the modules using pip install -e ., the scribe_data module will already be present in sys.path as it already has path to pip packages and then we do not need to add src/scribe_data explicitly to sys.path.
  • Official Guidelines for working on a Python package in development mode. Link
@shashank-iitbhu shashank-iitbhu added the bug Something isn't working label Mar 24, 2024
@shashank-iitbhu
Copy link
Contributor Author

I have already tested this locally. Let me know if these changes can help. Open for discussion.
cc @andrewtavis @wkyoshida
Should I open a PR, so that you can also test this?

@andrewtavis
Copy link
Member

andrewtavis commented Mar 24, 2024

Makes sense to me, @shashank-iitbhu! Feel free to send along a PR 🚀 I'll send along the structure changes right now, so if you could bring those into your branch before that'd be great 😊

@andrewtavis
Copy link
Member

2b72e64 just sent along the directory structure update, @shashank-iitbhu, so you're good to pull in the changes and send along a PR for this :)

@shashank-iitbhu
Copy link
Contributor Author

Opened a PR for these changes.
@andrewtavis This I believe highlights the need to include instructions for using pip install -e . in our contributing guide or documentation.

@Jk40git
Copy link
Contributor

Jk40git commented Mar 26, 2024

@andrewtavis and @shashank-iitbhu please is this issue still opened?

@shashank-iitbhu
Copy link
Contributor Author

@andrewtavis and @shashank-iitbhu please is this issue still opened?

I have linked a PR above, haven't been merged yet. It's still under review.

andrewtavis added a commit to shashank-iitbhu/Scribe-Data that referenced this issue Mar 29, 2024
andrewtavis added a commit that referenced this issue Mar 29, 2024
* remove modification of sys.path

Signed-off-by: Shashank Mittal <[email protected]>

* remove unnecessary imports

Signed-off-by: Shashank Mittal <[email protected]>

* ruff check

Signed-off-by: Shashank Mittal <[email protected]>

* #122 update docs with directions to pip install the local version

---------

Signed-off-by: Shashank Mittal <[email protected]>
Co-authored-by: Andrew Tavis McAllister <[email protected]>
@andrewtavis
Copy link
Member

Closed in #123 🚀 Thank you, @shashank-iitbhu!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

3 participants