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

load_pkgs doesn't work on Windows #97

Closed
M4thM4gician opened this issue Aug 19, 2022 · 5 comments · Fixed by #98
Closed

load_pkgs doesn't work on Windows #97

M4thM4gician opened this issue Aug 19, 2022 · 5 comments · Fixed by #98
Labels
bug Something isn't working

Comments

@M4thM4gician
Copy link
Contributor

Describe the bug
The load_pkgs method generates "permission denied" error when run on Windows 10 OS.
On Windows OS, the tempfile package that's used to create the temporary .in file doesn't allow the file to be written with a context manager because the NamedTemporaryFile function actually opens the file first. This prevents the open(tmp.name) and the pip-compile commands, which use this temporary file, from opening the file.

To Reproduce
Steps to reproduce the behavior:

  1. On Windows 10 OS, run vetiver.load_pkgs(path="YOUR PATH HERE")
  2. See error

Expected behavior
This should create a temp file, write the list of requirements from the model/list, then compile the temp file into a requirements.txt file.

Screenshots
image

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser chrome/edge

Additional context
This can be solved by closing the temporary file after it's opened (requires using delete=False when creating the temp file), and then deleting it after the requirements.txt file has been compiled. (see below)

def load_pkgs(model: VetiverModel = None, packages: list = None, path=""):
    """Load packages necessary for predictions

    Args
    ----
        model: VetiverModel
            VetiverModel to extract packages from
        packages: list
            List of extra packages to include
        path: str
            Where to save output file
    """

    required_pkgs = ["vetiver"]
    if packages:
        required_pkgs = list(set(required_pkgs + packages))
    if model.metadata.get("required_pkgs"):
        required_pkgs = list(set(required_pkgs + model.metadata.get("required_pkgs")))

    tmp = tempfile.NamedTemporaryFile(suffix=".in", delete=False)  # delete=False needed so file not deleted after closing
    tmp.close()  # Added to close file after created
    with open(tmp.name, "a") as f:
        for package in required_pkgs:
            f.write(package + "\n")

    os.system(f"pip-compile {tmp.name} --output-file={path}vetiver_requirements.txt")
    os.remove(tmp.name)  # Need to delete file after compiling completes
@isabelizimm isabelizimm added the bug Something isn't working label Aug 19, 2022
@isabelizimm
Copy link
Contributor

Thank you for this clear bug + fix for Windows users 🙌 would you want to open a PR with these changes? (If not, I am happy to implement it!)

@M4thM4gician
Copy link
Contributor Author

Will do!

@M4thM4gician
Copy link
Contributor Author

Sorry about closing and reopening. I needed to check the spacing for the newly added lines. All good now. Sorry again.

@isabelizimm
Copy link
Contributor

No worries, I appreciate the PR :D

@M4thM4gician
Copy link
Contributor Author

I used an incorrect variable in my last PR. I have submitted a new one with the correct variable (in the compile command within the load_pkgs function

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
None yet
Development

Successfully merging a pull request may close this issue.

2 participants