I found many questions with the same error, but none were addressing the actual cause of my problem so I am posting this. I am facing the issue in my (digital ocean) Linux production server.
I have python 3.5.2 in virtualenv, and python2.7.12 in the machine.
I have installed djangorestframework in virtualenv using command pip install djangorestframework
But did not install it in the actual machine (on 2.7)
python3 in the venv is working and importing rest_framework fine:
(venv) myname#server:/www/site$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rest_framework
>>> exit()
python2 in the machine is working and not importing rest_framework because its not installed.
myname#server:/www/site$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rest_framework
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rest_framework
>>> exit()
On running collectstatic command inside virtualenv I am getting the following error. It seems to be looking for the package in python2.7 and not inside virtualenv.
(venv) myname#server:/www/site$ sudo python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named rest_framework
My questing is that, Is it necessary for the package to be present in the server machine outside venv also?
If so, why? and Should I install rest_framework in the server on py2.7? If I do so, could it possibly break something?
(venv) myname#server:/www/site$ sudo python manage.py collectstatic
The issue is with the use of sudo here. It discards the environment to use the target account's one therefore discarding your virtual env.
If you want to keep it, you should likely use the -E flag to preserve the environment:
(venv) myname#server:/www/site$ sudo -E python manage.py collectstatic
you could also call the virtual env's python directly as well which would free you from setting up the visual env beforehand:
(venv) myname#server:/www/site$ which python
<path-to-python>
(venv) myname#server:/www/site$ sudo <path-to-python> manage.py collectstatic
Related
I have installed the tensorflow using pip in python 2.7.
When I try to test the tensorflow in python, all I get is this message:
ubuntu#ubuntu:~$ python
Python 2.7.15 |Anaconda, Inc.| (default, May 1 2018, 23:32:55)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/anto/anaconda2/lib/python2.7/site-packages/tensorflow/__init__.py", line 28, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "/home/anto/anaconda2/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "/home/anto/anaconda2/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 7, in <module>
from google.protobuf import descriptor as _descriptor
File "/home/anto/anaconda2/lib/python2.7/site-packages/google/protobuf/descriptor.py", line 113
class DescriptorBase(metaclass=DescriptorMetaclass):
^
SyntaxError: invalid syntax
>>>
I am currently running on:
ubuntu 16.04
ros1 kinematic
python 2.7
keras 2.6
tensorflow 1.14
protobuf 3.18.0
i have tried re-install a protobuf version of python 2.7 and tensorflow 1.x
this is all i get to solve this problem
encontered error in Tensorflow 1.8 Request you to uninstall by using
pip uninstall protobuf and try installing pip install protobuf==3.8
encontered error in Tensorflow 1.14 Request you to uninstall by using
pip uninstall protobuf and try installing pip install protobuf==3.9
it works for me
I hadnt any python installed globally,just miniconda. Then created a virtualenv(conda) and installed django there. Now when I do "django-admin.py startproject projectname" (anaconda prompt,under created virtualenv) first it asked how to open that file & I set it to default python of miniconda ("C:\Users\1\Miniconda3\python.exe").Then it shows:
Traceback (most recent call last):
File "C:\Users\1\Miniconda3\envs\Work_manager\Scripts\django-admin.py", line 2, in <module>
from django.core import management
ModuleNotFoundError: No module named 'django'
sys.path of that virtualenv:
['', 'C:\\Users\\1\\Miniconda3\\envs\\Work_manager\\python36.zip',
'C:\\Users\\1\\Miniconda3\\envs\\Work_manager\\DLLs',
'C:\\Users\\1\\Miniconda3\\envs\\Work_manager\\lib',
'C:\\Users\\1\\Miniconda3\\envs\\Work_manager',
'C:\\Users\\1\\Miniconda3\\envs\\Work_manager\\lib\\site-packages']
django-admin.py is located at "C:\Users\1\Miniconda3\envs\Work_manager\Scripts" &
its shebang is "#!C:/Users/1/Miniconda3/envs/Work_manager\python.exe".
Im using windows 10(x64).
I've read the many posts on SO regarding using Pillow or Image instead of PIL but I'm still having trouble with this module.
I'm running this on my MBP on 10.9.5.
I used pip to install image 1.5.5 and Pillow 3.4.2. However, when I go to import the module in my script, it keeps saying there's no module of that name.
first.last#localhost:/usr/local/bin> pip freeze | grep Pillow
Pillow==3.4.2
first.last#localhost:/usr/local/bin> pip freeze | grep image
image==1.5.5
first.last#localhost:/usr/local/bin> which pip
/usr/local/bin/pip
first.last#localhost:/usr/local/bin> which python
/usr/local/bin/python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named image
>>> import PIL
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>> import Pillow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Pillow
>>> import pillow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pillow
I checked /Library/Python/2.7/site-packages and it doesn't seem like the .py files are there for pillow or image, even though pip freeze showed that image and Pillow are both installed.
This is the contents of pip is this. Not sure if it gives any clues to why pip thinks the modules are installed but python says it can't find it.
#!/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==9.0.1','console_scripts','pip'
__requires__ = 'pip==9.0.1'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('pip==9.0.1', 'console_scripts', 'pip')()
)
From what I showed in my setup/environment, can anyone point me in a general direction to start fixing this problem? Is any additional information needed to debug this some more?
Since you are working outside of a virtualenv, the user site should be enabled. Use the following commands to find the installed location of image and/or pillow:
pip show -f image | grep Location
pip show -f pillow | grep Location
What you probably want to see is something like:
Location: /Users/<your_username>/.local/lib/python2.7/site-packages
If it's not installing there, then uninstall and pip install again with the --user flag.
Then use the following command to make sure that the results of above will be found in your path:
python -m site
What you want to see in the output is something like
sys.path = [
...
]
USER_BASE: '/Users/<your_username>/.local' (exists)
USER_SITE: '/Users/<your_username>/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True
If the installed location is in your user site, and your user site is enabled, then the import will work.
Okay so I have tried everything I and google can come up. I'm trying to run django-celery under a virtualenv on my Macbook Pro OSX 10.8.4. I installed django-celery using pip while the virtualenv was activated. I get the following when importing djcelery in virtualenv python.
(platform)Chriss-MacBook-Pro:platform Chris$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import djcelery
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Chris/Development/platform/lib/python2.7/site-packages/djcelery/__init__.py", line 25, in <module>
from celery import current_app as celery # noqa
ImportError: cannot import name current_app
I have tried to start a completely clean virtualenv but still seem to get the same error. I have tried upgrading the modules and have.
Here is also a list of my pip freeze packages.
BeautifulSoup==3.2.1
Django==1.5
MySQL-python==1.2.4c1
PIL==1.1.7
Pillow==2.0.0
amqp==1.0.12
anyjson==0.3.3
billiard==2.7.3.30
boto==2.9.6
celery==3.0.20
certifi==0.0.8
chardet==2.1.1
django-activelink==0.3
django-activity-stream==0.4.4
django-appconf==0.6
django-celery==3.0.17
django-compressor==1.2
django-debug-toolbar==0.9.4
django-flag==0.1.1
django-guardian==1.1.1
django-ses==0.4.1
django-tinymce==1.5.1
django-userena==1.2.0
easy-thumbnails==1.2
html5lib==1.0b1
jsonfield==0.9.13
kombu==2.5.12
oauthlib==0.1.3
pisa==3.0.33
pyPdf==1.13
pyasn1==0.1.7
python-dateutil==2.1
python-memcached==1.53
pytz==2013b
reportlab==2.7
requests==0.13.1
requests-oauth==0.4.1
rsa==3.1.1
six==1.3.0
vimeo-wrapper==0.0.2
wsgiref==0.1.2
Any help will be greatly appreciated!!
So it seems there was a clash between something in djcelery and my project folders.
The folder structure I was using was something like this
\-project
--manage.py
--requirements.txt
\---platform
----app1
----app2
----etc
It seems the platform folder was colliding with something in the import as soon as I changed this the import started functioning correctly.
Thanks for all the help and comments.
If you have a folder in your project called 'celery' rename it to something else. :)
Did you wrote into the settings.py?:
INSTALLED_APPS = (
...
'djcelery',
...
)
If you want work with Django's enviroment you must run console as
./manage.py shell
or
./manage.py shell --settings=path_to_settings_file_without_extention
I am doing this on Fedora
Problem:
(sandbox)[root#localhost mysite]# django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
Setup virtualenv
mkdir pythonenv # that's the /home/yeukhon/pythonenv/*.*
cd pythonenv
virtualenv --no-site-packages --distribute sandbox
Install Django
pip install -E sandbox django
# changing mode of /home/yeukhon/pythonenv/sandbox/bin/django-admin.py to 755
# Successfully installed django
Under /home/yeukhon/pythonenv/sandbox
bin include lib mysite
Under lib
You have /lib/python2.7/site-packages/django/*.*
Create Project is fine
(sandbox)[root#localhost sandbox]# django-admin.py startproject mysite
# the path is now /home/yeukhon/pythonenv/sandbox/mysite/*.*
Can't run server
django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
Python Shell under sandbox
(following this guide: How to troubleshoot - ImportError: Could not import settings 'mysite.settings' when deploying django?)
(sandbox)[root#localhost mysite]# python
Python 2.7.2 (default, Oct 27 2011, 01:36:46)
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE']
'mysite.settings'
>>> os.path.exists('/home')
True
>>> os.path.exists('/home/yeukhon/pythonenv/sandbox/mysite')
True
>>> os.path.exists('/home/yeukhon/pythonenv/sandbox/mysite/settings.py')
True
>>> from django.core.management import setup_environ
>>> import mysite.settings
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysite.settings
>>> setup_environ(mysite.settings)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mysite' is not defined
>>> print sys.path
['',
/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg',
'/home/yeukhon/pythonenv/sandbox/lib/python27.zip',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/plat-linux2',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-tk',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-old',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages'
What do I need to do to correct this problem? Thank you for your time.
EDIT
Thanks for the response.
I tried the following:
(sandbox)[root#localhost mysite]# export PYTHONPATH="/home/yeukhon/pythonenv/sandbox/"
(sandbox)[root#localhost mysite]# export PYTHONPATH="/home/yeukhon/pythonenv/"
(sandbox)[root#localhost mysite]# deactivate
[root#localhost mysite]# source ../bin/activate
(sandbox)[root#localhost mysite]# django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
>>> sys.path
['',.... '/home/yeukhon/pythonenv'.....]
It is now on the python path. But I still can't run the server.
Centralized Django Project
Yes. That's a good suggestion.
So I suppose that all I need to do is "create a directory called mydjango, then create projects within mydjango". But what commands need to be changed / added? I am willing to learn good practice.
Thank you very much.
Solution (Add to environment variable)
PYTHONPATH=$PYTHONPATH:path-to-your-directory
# PYTHONPATH=$PYTHONPATH:/home/yeukhon/pythonenv/sandbox/
The last line tells you all you need to know. In order to import mysite.settings, the parent directory of mysite must be on your PYTHONPATH. It currently isn't.
FWIW, it's not typical to actually store your project in the virtualenv directory. Usually, you put all your projects in on directory that you put on your PYTHONPATH. Then, just load up whatever virtualenv you need, and all's good. In fact, the best part of virtualenv is that they're interchangeable; i.e., you could easily run the same project in multiple different virtualenv environments (such as for testing a new release of Django without altering your normal virtualenv), but there again, you want your projects in one centralized place instead of inside a particular virtualenv directory.
Don't use django-admin.py for runserver, or indeed for anything other than startproject. Use manage.py runserver instead. That sets up all the relevant paths for you, and it should then just work.