Replacement in python package in Docker - python

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

Related

pylucene - ModuleNotFoundError: No module named 'org'

# Common imports:
import sys
from os import path, listdir
from org.apache.lucene.document import Document, Field, StringField, TextField
from org.apache.lucene.util import Version
from org.apache.lucene.store import RAMDirectory
from datetime import datetime
# Indexer imports:
from org.apache.lucene.analysis.miscellaneous import LimitTokenCountAnalyzer
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriter, IndexWriterConfig
# from org.apache.lucene.store import SimpleFSDirectory
# Retriever imports:
from org.apache.lucene.search import IndexSearcher
from org.apache.lucene.index import DirectoryReader
from org.apache.lucene.queryparser.classic import QueryParser
# ---------------------------- global constants ----------------------------- #
BASE_DIR = path.dirname(path.abspath(sys.argv[0]))
INPUT_DIR = BASE_DIR + "/input/"
INDEX_DIR = BASE_DIR + "/lucene_index/"
I'm trying test pylucene library. I have written this code only for import test. It doesn't work. I get
bigissue#vmi995554:~/myluceneproj$ cd /home/bigissue/myluceneproj ; /usr/bin/env /usr/bin/python3.10 /home/bigissue/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 36991 -- /home/bigissue/myluceneproj/hello_lucene.py
Traceback (most recent call last):
File "/home/bigissue/myluceneproj/hello_lucene.py", line 29, in <module>
from org.apache.lucene.document import Document, Field, StringField, TextField
ModuleNotFoundError: No module named 'org'
bigissue#vmi995554:~/myluceneproj$
I have run python3.10 -m pip list and there is "lucene" module. if I import lucene work well but python doesn't recognize org module. Why?
UPDATE
I downloaded lucene 9.1 and set environment variable (/etc/environment):
CLASSPATH=".:/usr/lib/jvm/temurin-17-jdk-amd64/lib:/home/bigissue/all_lucene/lucene-9.4.1/modules:/home/bigissue/all_lucene/lucene-9.1.0/modules" export CLASSPATH
I downloaded pylucene-9.1.0 and I have installed it
first jcc
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$ pwd
/home/bigissue/all_lucene/pylucene-9.1.0/jcc
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$python3.10 setup.py build
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$python3.10 setup.py install
I downloaded also ant apache.
then pylucene 9.1
cd ..
I have edit Makefile
vim /home/bigissue/all_lucene/pylucene-9.1.0/Makefile
PREFIX_PYTHON=/usr/bin
ANT=/home/bigissue/all_lucene/apache-ant-1.10.12
PYTHON=$(PREFIX_PYTHON)/python3.10
JCC=$(PYTHON) -m jcc --shared
NUM_FILES=10
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0: make
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0: make install
if I run python3.10 -m pip install | grep -i "lucene" I see it.
bigissue#vmi995554:~/all_lucene/pylucene-9.1.0$ python3.10 -m pip list | grep -i "lucene"
lucene 9.1.0
Now I have imported lucene
import sys
from os import path, listdir
from lucene import *
directory = RAMDirectory()
But I get
ImportError: cannot import name 'RAMDirectory' from 'lucene' (/usr/local/lib/python3.10/dist-packages/lucene-9.1.0-py3.10-linux-x86_64.egg/lucene/__init__.py)
Python doesn't use that kind of imports.
Just import lucene.
If this doesn't fix your problem, sorry!
You can use from lucene import whatever.
See the Features documentation, where it states:
"The PyLucene API exposes all Java Lucene classes in a flat namespace in the PyLucene module."
So, in Java you use import org.apache.lucene.index.IndexReader; but in PyLucene you use from lucene import IndexReader.
Update
Regarding the latest error you mentioned in the comments to your question:
ImportError: cannot import name 'RAMDirectory' from 'lucene'
Lucene's RAMDirectory has been deprecated for a long time - and was finally removed from version 9.0 of Lucene.
You can use a different directory implementation.
Recommended: MMapDirectory - but there are other options such as ByteBuffersDirectory
(Just to note, a new error/issue should really be addressed by asking a new question.)

OpenERP - modules import error

I'm using openerp without installation - just running it from source:
get openobject-server, openobject-addons and openerp-web from launchpad
place to /opt/openerp/
add to path to config file
addons_path = /opt/openerp/openobject-addons/,/opt/openerp/openerp-web/addons/
start openerp server with my config
All works
But now I want to install new addon (aeroo reports):
get aeroo sources
place to /opt/openerp/
change config
addons_path = /opt/openerp/openobject-addons/,/opt/openerp/openerp-web/addons/,/opt/openerp/aeroo/
It doesn't work
File "/opt/openerp/aeroo/report_aeroo/check_deps.py", line 33, in <module>
from osv import osv
ImportError: No module named osv
Same thing when I try to run it from sources under windows (using eclipse+pydev)
How can I make it see modules without changing code?
from osv -> from openerp.osv
from tools -> from openerp.tools
Not first time I see module with import without leading 'openerp.'
How can i fix it ?
Use this command into 7.0 openerp folder
find . -type f -print0 | xargs -0 sed -i 's/from osv/from openerp.osv/g'

Fabric - Running sed on a local file

I am trying to run sed on a file locally, here is my code:
from fabric.api import *
from fabric.contrib.files import sed
import os.path
def init(project, repository=None):
repository = project if not repository else repository
folder = os.path.join(os.path.dirname(__file__), repository)
local('cp -R bin/* %s' % folder)
with lcd(folder):
sed('wsgi.py', '{PROJECTNAME}', project)
It is then prompting me to specify a host. Is there any way I can run sed locally like this? I also tried:
local("sed -i \'s/{PROJECTNAME}/%s/\' wsgi.py" % project)
But I am getting the following error:
sed: -i may not be used with stdin
I don't know the sed contrib API, but fabric's docs say about the local function:
local is simply a convenience wrapper around the use of the builtin Python subprocess module with shell=True activated. If you need to do anything special, consider using the subprocess module directly.
So I suggest you just call subprocess.call() with shell=False, that should probably fix the error with sed -i
I managed to get this working by using the following:
local("sed -i \'\' -e\'s/{PROJECTNAME}/%s/\' wsgi.py" % project)
I am not sure why this works with the extra \'\' and what the implications are exactly, but it seems to work.
Presumably, the reason that this fixes your issue is that you're using Mac OS X (or another BSD).
The BSD version of sed requires the -i argument to have a value. That value should be the file extension that sed will use to create a backup, in case an error occurs during sed processing and the file needs to be rolled back to its original contents. The value can also be an empty string (''), indicating to sed that no backup file should be created.
GNU's version of sed is smarter, and knows that, if no value is passed, then no backup should be created. It does not need the empty string.

Django admin directory on Debian

Where can I find my Django admin template files (I am running Django on Debian)?
I tried to look here:
/usr/lib/python2.6/site-packages/django/contrib/admin/media/
But I can only get as far as /usr/lib. When I try to access python2.6 using cd command, I get message that it is not a directory.
I would appreciate any help :).
Try:
sudo updatedb
locate django/contrib/admin/media/
Go into the terminal and try this command...
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
This should inform you as to your site-packages location.
say if you want to see where a package -- say PIL -- is installed, get to a python shell, type:
>>> import PIL
>>> help(PIL)
The help file looks like:
Help on package PIL:
NAME
PIL
FILE
/usr/lib/python2.7/dist-packages/PIL/__init__.py
...
now I know that PIL is in /usr/lib/python2.7/dist-packages/PIL/
alternatively,
>>> print PIL.__file__
/usr/lib/python2.7/dist-packages/PIL/__init__.pyc

How can I add autocomplete to Vim for non-standard Python libraries?

For example, I have a Python script using the Google App Engine SDK:
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
The module db has a submodule Key, so I try to use autocomplete on it:
db.KTab
But at the bottom of the Vim window, I get the following:
-- Omni completion (^O^N^P) Pattern not found
How do I include the path to non-standard Python libraries so that Vim autocompletion can find them? And also display their docstrings?
You need to add your library files to your tags file. For instance, if you have installed the Google App Engine via pip in a virtual environment located in env/:
virtualenv --no-site-package env/
source env/bin/activate
pip install google_appengine
... then you should execute:
ctags -R --python-kinds=-i -o tags env/
If you did not install google_appengine through pip, then you should locate the path to your python libraries (hint: it should be indicated by $PYTHONPATH. And according to this reference page: "on Unix, this is usually .:/usr/local/lib/python.") and replace env/ by the path you found.
Finally, your .vimrc file should parse your tags file. For instance, in my .vimrc, I have:
set tags+=/path/to/my/tags
I grabbed this from natw's vimrc (I think...maybe sontek), but it should do the trick, so long as your packages are findable by your current install of Python. This lets you use gf, but also sets up searching these files for autocompletion. Note the py <<EOF part, which starts a section interpreted in Python. This means you'd have to have the python interpreter installed in vim to use it.
function! LoadPythonPath()
py <<EOF
# load PYTHONPATH into vim, this lets you hover over a module name
# and type 'gf' (for goto file) and open that file in vim. Useful
# and easier than rope for simple tasks
import os.path
import sys
import vim
for p in sys.path:
if os.path.isdir(p):
vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
EOF
endfunction
Btw, I don't like to have this load automatically, so I set it to a function that intelligently loads/unloads when I call it/first enter a Python doc. And I add a let g:PythonPathLoaded=1 to the previous function.
function! GetPythonPath()
if !exists("g:PythonPathLoaded")
call LoadPythonPath()
return
elseif g:PythonPathLoaded
return
else
call LoadPythonPath()
endif
endfunction
And I have an unload function too...though I'm not sure whether this makes a huge difference.
function! UnloadPythonPath()
py <<EOF
# load PYTHONPATH into vim, this lets you hover over a module name
# and type 'gf' (for goto file) and open that file in vim. Useful
# and easier than rope for simple tasks
for p in sys.path:
if os.path.isdir(p):
vim.command(r"set path-=%s" % (p.replace(" ", r"\ ")))
EOF
let g:PythonPathLoaded = 0
endfunction
Hope this helps! Plus, an added bonus is that this will load your packages regardless of whether you are using virtualenv (since it, I believe, runs whatever is set as 'python' at the moment).

Categories