JadyXuan / NTTS

NO TIME TO SLEEP
MIT License
627 stars 25 forks source link

[Question] I feel like this is a bit unnecessary #32

Closed demoliisher closed 1 month ago

demoliisher commented 1 month ago

In NTTS/ntts.py, Line 19 to 32.

def get_package_path():
    if hasattr(site, 'getsitepackages'):
        packages = site.getsitepackages()
        if len(packages) > 1:
            return packages[1]
        if len(packages) > 0:
            return packages[0]
    if sys.platform.startswith("linux"):
        return f"/home/{getpass.getuser()}/anaconda3/envs/torch/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages"
    elif sys.platform == "win32" or sys.platform == "cygwin" or sys.platform == "msys":
        return f"C:/Users/{os.getlogin()}/anaconda3/envs/torch/Lib/site-packages"
    elif sys.platform == "darwin":
        return f"/Users/{getpass.getuser()}/anaconda3/envs/torch/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages"
    return f"/usr/local/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages"

Why do you need to include platform judgements after the site.getsitepackages()? Does it operated differently in different platforms? Or didn't provide such a function in some environments?

t4wefan commented 1 month ago

The function get_package_path in your script is designed to return the path to the site-packages directory, where third-party packages are installed. The function first attempts to use site.getsitepackages() to find these directories. However, there are a couple of reasons why the platform-specific checks and paths are included after this initial attempt:

  1. Platform-Specific Differences:

    • The function site.getsitepackages() behaves differently across various operating systems. For example, the default paths for site-packages might differ between Linux, Windows, and macOS.
    • Different platforms might have different conventions for where Python environments are created and managed. For instance, on Windows, you might see paths like C:/Users/{username}/anaconda3/envs/torch/Lib/site-packages, while on Linux, it might be more like /home/{username}/anaconda3/envs/torch/lib/python{version}/site-packages.
  2. Anaconda Environments:

    • The code specifically checks for Anaconda environment paths, which are not necessarily handled correctly or consistently by site.getsitepackages(). Anaconda environments have their own structure and locations that might not be captured correctly by the default site package utility.
  3. Fallback Mechanism:

    • In cases where site.getsitepackages() does not return a valid or expected path (e.g., it might return empty or unexpected values), the platform-specific paths provide a fallback mechanism.
  4. Environment-Specific Scenarios:

    • There might be environments where site.getsitepackages() is not available or does not provide the correct information. This is common in custom installations or minimalist Python environments.

Here’s a breakdown of how the function addresses different scenarios:

This approach ensures robustness and flexibility, allowing the function to correctly identify the site-packages directory across various environments and configurations.

demoliisher commented 1 month ago

The function get_package_path in your script is designed to return the path to the site-packages directory, where third-party packages are installed. The function first attempts to use site.getsitepackages() to find these directories. However, there are a couple of reasons why the platform-specific checks and paths are included after this initial attempt:

  1. Platform-Specific Differences:

    • The function site.getsitepackages() behaves differently across various operating systems. For example, the default paths for site-packages might differ between Linux, Windows, and macOS.
    • Different platforms might have different conventions for where Python environments are created and managed. For instance, on Windows, you might see paths like C:/Users/{username}/anaconda3/envs/torch/Lib/site-packages, while on Linux, it might be more like /home/{username}/anaconda3/envs/torch/lib/python{version}/site-packages.
  2. Anaconda Environments:

    • The code specifically checks for Anaconda environment paths, which are not necessarily handled correctly or consistently by site.getsitepackages(). Anaconda environments have their own structure and locations that might not be captured correctly by the default site package utility.
  3. Fallback Mechanism:

    • In cases where site.getsitepackages() does not return a valid or expected path (e.g., it might return empty or unexpected values), the platform-specific paths provide a fallback mechanism.
  4. Environment-Specific Scenarios:

    • There might be environments where site.getsitepackages() is not available or does not provide the correct information. This is common in custom installations or minimalist Python environments.

Here’s a breakdown of how the function addresses different scenarios:

  • First Attempt: Try to use site.getsitepackages(). If this successfully returns one or more paths, use the first or second one as needed.
  • Second Attempt: If the platform is Linux, construct a path that follows typical Linux conventions for Anaconda environments.
  • Third Attempt: For Windows, construct a path based on typical Windows conventions for Anaconda environments.
  • Fourth Attempt: For macOS, construct a path based on typical macOS conventions for Anaconda environments.
  • Final Fallback: If none of the above work, return a general path that should work for a standard local Python installation.

This approach ensures robustness and flexibility, allowing the function to correctly identify the site-packages directory across various environments and configurations.

Thank you, it was my wrong I didn't read the string contents seriously.

t4wefan commented 1 month ago

Don't take this project too seriously, it's a satirical project to mock a company's embarrassing presentation at a press conference.

demoliisher commented 1 month ago

I think it is a good project to be an example of Change Traceback Output. 然后其实我是装外宾的(笑