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

Create requirements.txt if it doesn't exist #647

Merged
merged 2 commits into from
Dec 6, 2018

Conversation

lloydchang
Copy link
Contributor

This resolves:
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
The command '/bin/sh -c python3 -m pip install -r requirements.txt' returned a non-zero code: 1

This resolves:
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
The command '/bin/sh -c python3 -m pip install -r requirements.txt' returned a non-zero code: 1
@lloydchang lloydchang requested a review from a team as a code owner November 25, 2018 09:08
@StephenWeatherford
Copy link
Contributor

Thanks, will take a look soon. Can you recommend a simple python project that I could base a test off of?

This resolves:
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
The command '/bin/sh -c python3 -m pip install -r requirements.txt' returned a non-zero code: 1
@lloydchang lloydchang changed the title Create requirements.txt Create requirements.txt if it doesn't exist Nov 27, 2018
@lloydchang
Copy link
Contributor Author

You can use my hello-world at https://github.com/lloydchang/exercism-solutions/tree/master/python/hello-world

Here are my steps to reproduce the error:
A. In background, run: Docker daemon (e.g. Run Docker Desktop on a Mac)
B. From Terminal, load the hello-world into Visual Studio Code (e.g. cd hello-world && code .)
C. In Visual Studio Code, run:

  1. Command Palette (⇧⌘P)
  2. Docker: Add Docker Files to Workspace
  3. Select Application Platform: Python
  4. Which port does your app listen on: 3000
  5. Command Palette (⇧⌘P)
  6. Docker: Build Image
  7. helloworld:latest
  8. In Visual Studio TERMINAL: 1: Docker, it should display the error as follows:

docker build --rm -f "Dockerfile" -t hello-world:latest .
$ docker build --rm -f "Dockerfile" -t hello-world:latest .
Sending build context to Docker daemon 18.43kB
Step 1/7 : FROM python:alpine
---> aadc3feb2b19
Step 2/7 : LABEL Name=hello-world Version=0.0.1
---> Running in dc7ac6dedae8
Removing intermediate container dc7ac6dedae8
---> 6d35511ab82e
Step 3/7 : EXPOSE 3000
---> Running in 0dc87111fcc3
Removing intermediate container 0dc87111fcc3
---> 4a2d4bc74829
Step 4/7 : WORKDIR /app
---> Running in 60f8aeaca7e7
Removing intermediate container 60f8aeaca7e7
---> 145ec4b03cb2
Step 5/7 : ADD . /app
---> ebac5a2b6232
Step 6/7 : RUN python3 -m pip install -r requirements.txt
---> Running in 57c035381f7e
Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
The command '/bin/sh -c python3 -m pip install -r requirements.txt' returned a non-zero code: 1
$

Initial Context: I ran into the error while following Generating Docker files from https://code.visualstudio.com/docs/azure/docker

Writing Docker and docker-compose files by hand can be tricky and time consuming. To help you, VS Code can generate the necessary Docker files for your project. From the Command Palette (⇧⌘P), run the Docker: Add Docker files to Workspace command to generate Dockerfile, docker-compose.yml, and docker-compose.debug.yml files for your workspace type:

Additional Contexts: In Visual Studio Python tutorials, Create a requirements.txt file for the environment is an optional activity; please see:
• https://code.visualstudio.com/docs/python/tutorial-django#_optional-activities
https://code.visualstudio.com/docs/python/tutorial-flask#_optional-activities

Considering requirements.txt may (be optionally created manually) or may not exist (in a hello-world simple python project), I amended my pull request to create requirements.txt if it doesn't exist:
RUN if [ ! -f requirements.txt ]; then pip3 freeze > requirements.txt; fi

@@ -32,6 +32,7 @@ WORKDIR /app
ADD . /app

# Using pip:
RUN if [ ! -f requirements.txt ]; then pip3 freeze > requirements.txt; fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be added to the commented out sections for pipenv and miniconda as well?

@@ -32,6 +32,7 @@ WORKDIR /app
ADD . /app

# Using pip:
RUN if [ ! -f requirements.txt ]; then pip3 freeze > requirements.txt; fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to run the built image, I get this:

docker run --rm -it -p 3000:3000/tcp hello-world:latest
/usr/local/bin/python3: No module named hello-world

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so it looks like we're picking up the module name from the name of the folder the project is in (in this case hello-world). So I guess it's by design for the current implementation, although we do a better job here on other languages. Is there an official way to properly pick this up (e.g. from a file in the project folder), for possible use later? Thx.

@StephenWeatherford
Copy link
Contributor

@brettcannon Brett, other than the questions above, this change looks good to me. Do you have any reservations? Thanks!

@StephenWeatherford
Copy link
Contributor

@lloydchang I'm going to go ahead and merge this so we can get it into the next release. If you'd like to make updates or provide more info according to above comments, just let me know. Thanks!

@StephenWeatherford StephenWeatherford merged commit d4c1b3e into microsoft:master Dec 6, 2018
@brettcannon
Copy link
Member

Sorry for not getting back sooner but I was travelling last week.

So I actually disagree with this change as freezing should be an explicit step the developer takes, otherwise you aren't making a conscious decision of what dependencies you do (or do not) want to end up in your image.

And if the decision to keep this line stands then it should be updated to follow the line it precedes, e.g. python3 -m pip freeze.

@StephenWeatherford
Copy link
Contributor

StephenWeatherford commented Dec 10, 2018

No problem, thanks for letting me know. I would rather show (or document) best practice first, and have it be convenient second. Does it make sense to only supply the requirements.txt file to install if it exists? Or is it better to supply it always and document that users should create it? Or something else? Thanks!

@StephenWeatherford
Copy link
Contributor

@brettcannon, @lloydchang Moved the discussion to #693. Reverting the change for the time being.

StephenWeatherford added a commit that referenced this pull request Dec 11, 2018
@microsoft microsoft locked and limited conversation to collaborators Oct 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants