I'm trying to add object-persistence to my python app. I'm currently using python 3.4 on windows.
The only module I found useful was shelve but on windows, it appears, it creates three different files rather than one: 'db.bak' ,'db.dat' and 'db.dir'.
How can I force shelve to create only single file ?
furthermore , whenever I need to open that db on read-only mode , 'db.dir' is being edited for some reason.
Does anyone know how to fix those issues ? or maybe suggest other modules that would work considering the OS and python version?
Related
I created a Python package that is actually a command line interface. Everything works fine, but I would like to be able to store simple user settings (a few values) in some file (json, yaml, or whatever). Handling such files itself is not a problem, but I don't know where I could store them. My program can be installed using pip install https://github.com/repo and works fully off-line.
It seems that Python does not allow me to store settings files in the same folder, where the compiled program itself is located (Programs\Python\Python38\Scripts) which makes sense, but I don't know how to do it any other way.
Inspired by the comments under the question, I searched a little bit more and found the answer to my problem.
I store my settings file in C:\users\me\appdata\local\programs\python\python38\lib\site-packages\my_package\config.json which can be accessed with
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.json')
I haven't tested this on Linux or macOS yet, but I think it will work.
EDIT: Unfortunately it does not work on Linux without sudo. For this reason, I used the following:
os.path.join(os.path.expanduser("~"), '.my_package_name', 'config.json')
I am sorry if this question has already been answered, however it is a topic very little discussed about.
I am trying to run a macro in libreoffice. The macro has been written in python as shown.
import uno, os.path, sys
import pandas
def Bring_from_doc():
doc = XSCRIPTCONTEXT.getDocument()
siz=doc.Sheets
uno, os and sys can be imported without any issue since they are installed in libreoffice python installed folder.
However pandas is not installed and got this error when running script:
This is the directory where libreoffice python libraries are located including uno, os and sys. But pandas and other wanted are not.
My question is: How can be installed pandas and any other required library that can be used by any python script run by libreoffice in a macro?
Thank you!!
On Linux, this is easy. Simply install the library in the system python and it will work from a LibreOffice macro. Verify whether it is python 2 or 3 on your system; for example on Ubuntu, normally I enter python3 as the executable name.
On Windows, this is nearly impossible and I would not recommend it (and have repeatedly stated this on stackoverflow, as it is asked here every so often). Numerous environmental variables must be set correctly and other hacks are required. If you are an expert then it can be done, but since you are asking here, my guess is that you do not have the required skills to make this process go smoothly!
Even if you do get it to work, no one else will be able to use your macro, and you would need to do it all over again if you use a different computer. So I would not recommend it in most cases even for experts.
Alternatives:
Run Linux in a virtual machine.
Install a normal distribution of python elsewhere on your system, and write a normal python script that imports pandas, saving the result for example to an xml file. When that script is finished, run a LibreOffice macro that reads the result file and does not import pandas.
Avoid using specialized libraries at all. This is what I normally do, as the standard python libraries allow you to do quite a bit, maybe not as easily, but you could import some extra code or write workarounds to do what is needed.
I know similar questions have been popular in the past, but none refers to my problem. I'm looking for a way to read data from Excel file in Python, but I'm strongly against using non-builtin modules.
The reason why is that in my case Python is a component of another software, so incorporating additional module would require from every user knowledge about how to use pip, which Python installation on your pc should one install module into, etc. The solution must not require any additional actions from user.
I can read CSV files with Python builtin easily, so that could work, but how can I convert Excel to CSV in the first place? Or is there a way to read Excel directly?
Edit: It is Python 2, that is used in this software.
Edit2:
Anyone minds explaining the downvote? I think this isn't a question about a ready solution or module, but rather a method and is well detailed. It is not always possible to use external modules, so this is an actual problem. If it is not possible at all though, then I would simply expect an answer instead of -1.
Not really the prettiest solution, but you could download the complete code repository of one of the excel handling packages for python (openpyxl for example) and put these files in the same directory as the python script that you're going to run. Subsequently you can do an import of these local package files in your script.
Note: if the excel handling package has dependencies on other packages, then you'll need to download these as well.
I'm making an installation package which is to be used on windows and includes a sqlite3 database (which is not read-only).
When then package has been installed the sqlite3 database has become read-only for some reason. By the way, I'm using python 2.7.3 (with sqlite3 lib) to read/write from it.
My question is, is there away for me to unlock the sqlite database to become read/write through a python script, bat script, or the inno setup script perhaps?
Or is there a way for me to modify my inno setup script to keep the sqlite database from becoming read-only in the first place?
I've tried searching the forums and googling for an answer but haven't succeeded in finding one.
THanks in advance!
On Windows, anything inside the Program Files folder is meant to be read-only during normal use. Data files should be installed elsewhere. See here for more advice on where to put them:
Does Microsoft have a best practices document regarding the storage of App Data vs User Data on different Windows Platforms?
I'm trying to use PyUNO as a method to convert different document formats (doc, wordperfect, html, etc) to PDF from within my Django server. I'm having a heck of a time getting import uno to work. It seems to fail when doing import pyuno, with a message of ImportError: DLL Load Failed: The specified module could not be found.
The only way I can get this to work is to use the Python 2.6 that came with OpenOffice, but I really want to use my other 2.6 installation. The docs for PyUNO are all for Python 2.2, and reputed to be out-of-date.
I'm guessing that some (or all) of the following files need to be copied from the OpenOffice directory to my site-packages directory (or some subdirectory thereof):
pythonloader.py
pythonloader.uno.ull
pythonloader.uno.ini
pythonscript.py
pyuno.pyd
Has anyone had any success getting this to work?
This is on Windows.
For simply conversions, you needn't reinvent the wheel. Look at unoconv: http://dag.wieers.com/home-made/unoconv/
'Import uno' will automatically work IF the python interpreter was bundled with OpenOffice, or in some Linux systems where the packagers have done a lot of work for you already.
Alternative 1: For other Python installs on Win32 systems, you need to import three environment variables and add one item to your Pythonpath. The detailed tutorial is at http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=36370&p=166783
The three environment variables you must get FROM your OO-installed-Python and add TO your other install of Python are:
(Using Python 2.6 and OO 3.1.2)
os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:c:\Program Files\OpenOffice.org 3\program\fundamental.ini'
os.environ['UNO_PATH'] = 'c:\Program Files\OpenOffice.org 3\program\'
os.environ['PATH'].append('c:\Program Files\OpenOffice.org 3\URE\bin;c:\Program Files\OpenOffice.org 3\Basis\program;')
The pythonpath item you must add TO your other install of Python is the location of the uno module:
sys.path.append('C:\Program Files\OpenOffice.org 3\Basis\program')
Now you can simply 'import uno'.
Pyuno is only compatible with a similar version of Python. Since OO 3.1 bundles Python 2.6.1, that pyuno is only compatible with another Python 2.6. Attempting to import uno into a different version of Python will cause a runtime error. But there's a way around that in Alternative 2.
Alternative 2: For other Python installs on WIN32 systems, you can ignore the Python-UNO bridge completely and use the Python-COM bridge instead. You must install one new module, and the API has a few differences, but you can use ANY version of Python, including Python3.
Install the pywin32module to get COM access: http://sourceforge.net/projects/pywin32/
Discussion on the API differences: http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=36608&p=168179
I also need an answer to this. I'm using windows XP and have a system where I extract data from a SQL server DB and insert it into template excel files. I DON'T want to add macro's etc to the OpenOffice. but I want to use the service to produce these files.
However, the only way I seem to be able to import the uno modude is to use the python.exe from the openoffice directory. even then I can't associate this exec with the py files in my system for some reason, thus I have to put in full path names each and every time.
I know that in Linux, the Pyuno module is an addon and can be used in normal python, but in windows this doesn't seem to be the case. I have listed all the relevant path details from the python exec under openoffice and duplicated them in python 2.7, but pyuno still fails with unable to find DDL with no reference to what DDL.
I think the answer is "this is not possible." From other reading on the web, it appears that the stdlib used to compile/link the python executable from python.org is different from the stdlib used to compile/link the python.exe distributed with OpenOffice.org. I don't know why, and I'm still confused by the fact that both pythons give me the same startup messages. So I could be completely on the wrong track here.