9

I have a Heroku app that's running Node, but I need to be able to run Python scripts on this server. I'm trying to install my Python dependencies but can't get it to work.

I've added both python and node build packs to my project, I've create a virtual environment and successfully installed requirements.txt, but I continue to get module not found errors.

How can I properly install Python packages on a Heroku Node server?

Heroku shows that I have correctly set both buildpacks:

heroku buildpacks --app <my app>

Outputs:

1. heroku/nodejs
2. heroku/python 

If I try to install requirements.txt:

$ pip install -r requirements.txt

It says the requirements are already satisfied. I'm guessing this is in reference to my local environment.

Requirement already satisfied (use --upgrade to upgrade): requests==2.7.0 in /Library/Python/2.7/site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4==4.5.3 in /Library/Python/2.7/site-packages (from -r requirements.txt (line 2))

But if I try to run my Python script, I get a no module error:

Traceback (most recent call last): File "Webcrawler.py", line 3, in from urllib.request import urlopen ImportError: No module named request

3
  • Have you tried this solution - stackoverflow.com/questions/36167012/… ? Commented Mar 10, 2017 at 16:43
  • @hashcode55 Yes, I have tried that, but I believe it's an outdated answer as running that doesn't generate the .buildpacks file. I tried creating it manually, but that didn't work. Commented Mar 10, 2017 at 18:02
  • @123 Can you show build log from Heroku? Commented Mar 10, 2017 at 23:30

2 Answers 2

9

I've solved it so:

  1. I created (If there is not) the file "requirements.txt" directly into the root of the entrypoint (index.js, in my case)
  2. I manually added the libraries with the specific versions into the file, in my case like this:

    requests==2.7.0
    beautifulsoup4==4.5.3
    
  3. I created (If there is not) the file "runtime.txt" with with just one line: the python version i need, in my case:

    python-2.7.14
    
  4. I created the multibuildpacks with the two commands:

    heroku buildpacks:set heroku/python
    heroku buildpacks:add --index 1 heroku/nodejs
    
  5. Finally, I continued with the git commands:

    git add requirements.txt runtime.txt
    git commit requirements.txt runtime.txt -m "requirements"
    git push heroku master
    
Sign up to request clarification or add additional context in comments.

Comments

0

You probably need to use multiple buildpacks. See:

In the Heroku Dev Center.

1 Comment

This is exactly what I've done. As I said in the question, I have added the appropriate build packs to my project, but pip still tries to install on my local machine unless I use virtualenv. I could successfully install the dependencies using virtualenv, but it doesn't resolve the no module errors for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.