Django import error - no module named django.conf.urls.defaults - python

I am trying to run statsd/graphite which uses django 1.6.
While accessing graphite URL, I get django module error
File "/opt/graphite/webapp/graphite/urls.py", line 15, in
from django.conf.urls.defaults import *
ImportError: No module named defaults
However, I do not find defaults django package inside /Library/Python/2.7/site-packages/django/conf/urls/
Please help fixing this issue.

django.conf.urls.defaults has been removed in Django 1.6. If the problem was in your own code, you would fix it by changing the import to
from django.conf.urls import patterns, url, include
However, in your case the problem is in a third party app, graphite. The issue has been fixed in graphite's master branch and version 0.9.14+.
In Django 1.8+ you can remove patterns from the import, and use a list of url()s instead.
from django.conf.urls import url, include

If for some reason you don't want to downgrade to Django 1.5.x or upgrade Graphite then you can apply the fix to your older Graphite with:
find ./ -type f -exec sed -i -e 's/from\ django\.conf\.urls\.defaults\ import\ \*/from\ django\.conf\.urls\ import\ \*/g' {} \;
..in your <graphite_dir>/webapp/graphite dir.
This helped me with my Graphite 0.9.12 and Django 1.7(.5).
(I also had to do:
find ./ -type f -exec sed -i -e 's/mimetype\=/content_type\=/g' {} \;
find ./ -type f -exec sed -i -e 's/content_type\=mimetype/content_type\=content_type/g' {} \;
..later on as after I managed to start Graphite some of its features didn't work. Now they work for me but YMMV.)

go to the file location where you have installed python.
open cmd on that path and then install django with command >> pip install django
Then cross check on python shell with import django(which should do nothing)
or simply use the command >> python -m django --version
it will simply give you version
enter image description here
enter image description here

Related

Replacement in python package in Docker

GraphQL is still not supported in Django 4, so to use it I need to change the line:
"from django.utils.encoding import force_text"
to
"from django.utils.encoding import force_str as force_text"
in package
"VENV/lib/PYTHON_VERSION/site-packages/graphene_django/utils/utils.py"
The problem occurs when using Docker, how could I replace this line when building the container?
Instead of manually changing the python module in site-packages, and assuming there's no other way to fix this (i.e. if all you need is force_text to be defined at django.utils.encoding), you could also write a "monkey patch", i.e. a runtime patch of the django.utils.encoding module. Adding something like this in our own code (untested):
from django.utils import encoding
encoding.force_text = encoding.force_str
Later, once not needed, this patch should be removed.
Simply combine RUN with a sed-replace in your dockerfile.
RUN pyver="python$(python --version | grep -Eo '[0-9]\.[0-9]')" && \
sed -i "s/import force\_text/import force\_str as force\_text/g" \
./lib/$pyver/site-packages/graphene_django/utils/utils.py
Replace <PATH_TO_utils.py> with the path to utils.py

How to get the location of installed Python package into the shell

I want my users to be able to reference a file in my python package (specifically a docker-compose.yml file) directly from the shell.
I couldnt find a way to get only the location from pip show (and grep-ing out "location" from its output feels ugly), so my current (somewhat verbose) solution is:
docker compose -f $(python3 -c "import locust_plugins; print(locust_plugins.__path__[0])")/timescale/docker-compose.yml up
Is there a better way?
Edit: I solved it by installing a wrapper command I call locust-compose as part of the package. Not perfect, but it gets the job done:
#!/bin/bash
module_location=$(python3 -c "import locust_plugins; print(locust_plugins.__path__[0])")
set -x
docker compose -f $module_location/timescale/docker-compose.yml "$#"
Most of the support you need for this is in the core setuptools suite.
First of all, you need to make sure the data file is included in your package. In a setup.cfg file you can write:
[options.package_data]
timescale = docker-compose.yml
Now if you pip install . or pip wheel, that will include the Compose file as part of the Python package.
Next, you can retrieve this in Python code using the ResourceManager API:
#!/usr/bin/env python3
# timescale/compose_path.py
import pkg_resources
if __name__ == '__main__':
print(pkg_resources.resource_filename('timescale', 'docker-compose.yml'))
And finally, you can take that script and make it a setuptools entry point script (as distinct from the similarly-named Docker concept), so that you can just run it as a single command.
[options.entry_points]
console_scripts=
timescale_compose_path = timescale:compose_path
Again, if you pip install . into a virtual environment, you should be able to run timescale_compose_path and get the path name out.
Having done all of those steps, you can finally run a simpler
docker-compose -f $(timescale_compose_path) up

no module name pip : after changing permission of python folder

here is what i did before i messed up everything :
i tried to install a package using pip3 , after a long time the download finished and suddenly the error about permission came up because i forgot to use sudo at first and because I didn't want to download the packages again and didn't know where is the pip cache folder is , I did a very stupid thing i changed the permission of the entire python folder in the /usr/bin/ to install package without sudo , after this i tried this :
pip3 install tensorflow
File "/usr/bin/pip3", line 7, in <module>
from pip import main
ImportError: No module named 'pip'
i got these damn error ,
can anybody help me fix this ?
Edit :
here is my sequence of the command i used :
1 - pip3 install tensorflow
-- the error came up
2 - sudo find /usr/lib/python3.5/ -type d -exec chmod 766 {} \;
3 - sudo find /usr/lib/python3.5/ -type f -exec chmod 766 {} \;
First, and foremost, I consider your approach quite unwise. You have now changed the permissions of all files and directories for the owner, the group, and others.
In principle you just needed to ensure that pip3 (by extension, your user-account) would be able write files and directories in a directory owned by root (presumably /usr/lib/python3.5/site-packages). You could have accomplished this by:
sudo chmod o+w /usr/lib/python3.5/site-packages
Alternatively you could have changed the ownership of this folder. IMPORTANT: when doing this kind of thing, be sure to know what you are doing, and don't forget to change everything back as soon as possible. Things can be broken, and security issues can be created.
Now as to a solution to your problem. You have now given the directories the following permissions -rwxrw-rw- (6 = 4 (read) + 2 (write)). However for users, and programs executed on its behalf, to do anything to/from a directory they need the right to execute. For this you should have used 5 instead of 6 (5 = 4 (read) + 1 (execute)). To correct:
sudo find /usr/lib/python3.5/ -type d -exec chmod 755 {} \;
Then, I think that for Python to correctly load compiled libraries (shared-objects or .so files) they should also have these permissions. Judging from my own Python directory I would probably do:
sudo find /usr/lib/python3.5/ -type f -exec chmod 644 {} \;
sudo find /usr/lib/python3.5/ -type f -iname '*.so' -exec chmod 755 {} \;
to set everything back to it's original state.
P.S. I am no expert in pip, so I have no idea what the protocol is to avoid pip from re-downloading upon retrying a failed installation.

tensorflow-syntaxnet: Where are sentence_pb2 and gen_parser_ops?

I cannot import these two python files from syntaxnet and I cannot find them either. Does anyone know how to solve this issue? Thanks!
from syntaxnet.ops import gen_parser_ops
from syntaxnet import sentence_pb2
Those two are imported in syntaxnet/syntaxnet/conll2tree.py. You can get their locations by adding lines like below in conll2tree.py:
import os
print os.path.realpath(gen_parser_ops.__file__)
and then run the demo.sh as in the installation guide.
gen_parser_ops file is present under bazel-out/local-opt/genfiles/syntaxnet/ops/gen_parser_ops.py
sentence pb2 file is present in bazel-out/local-opt/genfiles/syntaxnet/sentence_pb2.py.
I found these files in my linux machine (ubuntu) using the find command:
sudo find ~/ -type f -name "gen_parser_ops*"
sudo find ~/ -type f -name "sentence_pb2*"

Patching broken file

In my travis setup I want to apply a patch to a very specific python file that is broken on some configurations. The installation procedure looks like this:
install:
# test with various Django versions
- pip install Django==$DJANGO
# patch Django 1.4 libgeos
- 'if [ $DJANGO == "1.4.13" ]; then find . -name libgeos.py -exec patch {} travis/geos-dev.patch \; ; fi'
But apparantly, find is not able to retrieve the file in question.
How can I realiably find this file, also across different python versions?
Seems like a simple find under /home/travis/virtualenv/ does the job.

Categories