简体   繁体   中英

Django installed but throws import error in virtual env when running: sudo python manage.py collectstatic

I am working in a virtual environment. When I run import django and django.VERISON, I get

>>> django.VERSION
(1, 10, 2, 'final', 0)

but when I run the command

sudo python manage.py collectstatic

it throws error:

"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

I checked the sys.path inside virtual env and it does not have django. it has these:

['', '/www/XXX/venv/lib/python35.zip', '/www/XXX/venv/lib/python3.5', '/www/XXX/venv/lib/python3.5/plat-x86_64-linux-gnu', '/www/XXX/venv/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/www/XXX/venv/lib/python3.5/site-packages']

I still don't know how to make it work. Should I install django again in virtualenv, or should I uninstall the version already there on the server, or should I just add it to python variable? Also I if should do one of these things, how to do it?

I am working with a server for the first time, hence not doing anything intuitively.

EDIT: I am inside a virtual environment

While trying to install django in venv I get:

(venv) ritambhara@XXX-backend:/www/XXX$ pip install Django==1.10.2
Requirement already satisfied: Django==1.10.2 in ./venv/lib/python3.5/site-packages

when running the command with sudo, I get:

(venv) ritambhara@XXX-backend:/www/XXX$ sudo python manage.py collectstatic
Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

when running the command without sudo, I get:

(venv) ritambhara@XXX-backend:/www/XXX$ python manage.py collectstatic
Traceback (most recent call last):
  File "/usr/lib/python3.5/logging/config.py", line 558, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib/python3.5/logging/config.py", line 731, in configure_handler
    result = factory(**kwargs)
  File "/usr/lib/python3.5/logging/handlers.py", line 150, in __init__
    BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
  File "/usr/lib/python3.5/logging/handlers.py", line 57, in __init__
    logging.FileHandler.__init__(self, filename, mode, encoding, delay)
  File "/usr/lib/python3.5/logging/__init__.py", line 1008, in __init__
    StreamHandler.__init__(self, self._open())
  File "/usr/lib/python3.5/logging/__init__.py", line 1037, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: '/www/XXX/logs/log.log'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/www/XXX/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/www/XXX/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()XXX
  File "/www/XXX/venv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/www/XXX/venv/lib/python3.5/site-packages/django/utils/log.py", line 75, in configure_logging
    logging_config_func(logging_settings)
  File "/usr/lib/python3.5/logging/config.py", line 795, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python3.5/logging/config.py", line 566, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'file_log': [Errno 13] Permission denied: '/www/XXX/logs/log.log'

Based on your updated question, it looks like your virtual environment got corrupted somehow. That's ok, they are designed to be easily replaceable.

Create a new virtual environment:

python -m venv /www/XXX/venv2

Activate it:

. /www/XXX/venv2/bin/activate

Install Django (and other dependencies) with pip , for example:

pip install django

After this you will be able to run Django commands, and you should not use sudo .

Django has to be installed on the virtual environment on which we work. Installing it outside of the virtual environment has no use. After installing Django in the virtual envirnonment use

pip freeze

This will show the version of the installations. Then for managing static files the command is

python manage.py collectstatic

I reinstalled everything from start but it did not solve the problem. also I tried using a new virtual environment but it threw the same error.

Finally I restarted the server using sudo /etc/mod_wsgi-express-80/apachectl restart , and it worked. I don't know why exactly.

Also DO NOT use sudo with command python manage.py collectstatic , thanks Janos

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM