Asked  7 Months ago    Answers:  5   Viewed   349 times

I'm deploying a Django app to a dev server and am hitting this error when I run pip install -r requirements.txt:

Traceback (most recent call last):
  File "/var/www/mydir/virtualenvs/dev/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

pkg_resources appears to be distributed with setuptools. Initially I thought this might not be installed to the Python in the virtualenv, so I installed setuptools 2.6 (same version as Python) to the Python site-packages in the virtualenv with the following command:

sh setuptools-0.6c11-py2.6.egg --install-dir /var/www/mydir/virtualenvs/dev/lib/python2.6/site-packages

EDIT: This only happens inside the virtualenv. If I open a console outside the virtualenv then pkg_resources is present, but I am still getting the same error.

Any ideas as to why pkg_resources is not on the path?

 Answers

47

July 2018 Update

Most people should now use pip install setuptools (possibly with sudo).

Some may need to (re)install the python-setuptools package via their package manager (apt-get install, yum install, etc.).

This issue can be highly dependent on your OS and dev environment. See the legacy/other answers below if the above isn't working for you.

Explanation

This error message is caused by a missing/broken Python setuptools package. Per Matt M.'s comment and setuptools issue #581, the bootstrap script referred to below is no longer the recommended installation method.

The bootstrap script instructions will remain below, in case it's still helpful to anyone.

Legacy Answer

I encountered the same ImportError today while trying to use pip. Somehow the setuptools package had been deleted in my Python environment.

To fix the issue, run the setup script for setuptools:

wget https://bootstrap.pypa.io/ez_setup.py -O - | python

(or if you don't have wget installed (e.g. OS X), try

curl https://bootstrap.pypa.io/ez_setup.py | python

possibly with sudo prepended.)

If you have any version of distribute, or any setuptools below 0.6, you will have to uninstall it first.*

See Installation Instructions for further details.


* If you already have a working distribute, upgrading it to the "compatibility wrapper" that switches you over to setuptools is easier. But if things are already broken, don't try that.

Tuesday, June 1, 2021
 
keisar
answered 7 Months ago
64

By default, App Engine looks for an app variable in a file called main.py. You have two options: put your WSGI app where App Engine expects it to be, or define a custom entrypoint:

Put your WSGI app where App Engine expects it to be:

You can create a file called main.py that has an app variable which is simply imported and aliased from the correct location:

from demosite.wsgi import main as app

Adding a custom entrypoint:

From https://cloud.google.com/appengine/docs/standard/python3/config/appref:

entrypoint: Optional. The command that is executed when your app starts. For your app to receive HTTP requests, entrypoint should contain a command which starts a web server that listens on the port specified by the PORT environment variable. If you do not specify an entrypoint, App Engine will configure and start the Gunicorn webserver.

By default it's this:

entrypoint: gunicorn -b :$PORT main:app

You would need something like:

entrypoint: gunicorn -b :$PORT demosite.wsgi:main

See here for more details about application startup: https://cloud.google.com/appengine/docs/standard/python3/runtime#application_startup

Saturday, July 31, 2021
 
VinoCoder
answered 4 Months ago
16

I finally resorted to using conda to set up an environment rather than virtualenv. Apparently virtualenv is not compatible with anaconda.

Info in this link helped me set it up:

https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/20/conda/

Tuesday, August 3, 2021
 
Craig
answered 4 Months ago
49

Here's the full code including the __init__.py

File - db/__init__.py

from .DataSource import *
from .RecordSet import * 

File - main/__init__.py

from .main import *

File - PQF/__init__.py

from .db import *
from .main import *
from db import DataSource as database
from db import RecordSet

def main():
   print("hello")

Run the main.py script as python3 -m main.main

Tuesday, August 31, 2021
 
lauda
answered 3 Months ago
89

It doesn't really matter where you store your virtual environment. Find a project structure that works for you.

I wouldn't put the virtual env inside the project, because you shouldn't check it into version control (although you could use an ignore). Normally, you just need to check in your requirements file, so that you can recreate the environment.

I wouldn't put the project inside the virtual env, because virtual envs are disposable. You might want to destroy the virtual env without destroying the project. Also, you might want to run the same project under different virtual envs e.g. test your code on Django 1.8 and 1.9 before upgrading.

You might find virtualenvwrapper useful. It has some tools that make it easy to create and switch between virtual environments. It stores all of your virtual environments in one place, so you don't have to worry about where to put them.

Is this correct or can I do it without activating virtual environment first?

You should activate the virtual environment and install django before you create / work on your project.

Sunday, September 19, 2021
 
Bruce
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share