How do I specifiy the home for a Berkeley DB DBEnv? - python

I want to use a Berkeley DB in the concurrent read mode, so in a python interpreter (Python 2.7.8 64-bit on Win7) I did this:
import os
from bsddb import db
os.chdir(os.path.expanduser('~'))
os.mkdir('DB')
home_dir = os.path.join(os.getcwd(), 'DB')
dbenv = db.DBEnv()
dbenv.open(home_dir, db.DB_INIT_CDB|db.DB_INIT_MPOOL, 0)
which gives me this error
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
dbenv.open(home_dir, db.DB_INIT_CDB|db.DB_INIT_MPOOL, 0)
DBNoSuchFileError: (2, 'No such file or directory')
What am I doing wrong?

When opening a database for the first time, specify the DB_CREATE flag in addition to the others you've listed.

Related

What is the proper way to transfer a file over a network with the shutil.copy module?

I am a novice programmer so please excuse the poor code and the simplicity of my inquiry. I am attempting to transfer an SQL database backup from one machine to another via Python. I am using Python version 3.6.4 on a Windows 7 Professional machine. The machine I'm transferring it to is a Windows 10 machine. Now, when I enter in the following code line by line in the Python shell, it works with no issue.
import os, pyodbc, shutil
connection = pyodbc.connect(driver = '{ODBC Driver 13 for SQL Server}',
server = 'SERVER',
database = 'DATABASE',
UID = 'USER',
PWD = 'PASS',
autocommit = True)
connection.cursor().execute(
r"backup database DATABASE to disk = 'c:/DB/backup/DATABASE.bak';")
connection.close()
os.system(r'net use \\0.0.0.001\DB\backup /user:DOMAIN\USER PASS')
os.system(r'net use \\0.0.0.100\d\Backups /user:DOMAIN\USER PASS')
file = 'DATABASE.bak'
src = os.path.join('\\\\0.0.0.001','DB','backup', file)
dst = os.path.join('\\\\0.0.0.100','d','Backups')
shutil.move(src, dst)
os.system(r'net use * /delete /y')
However, when I compile the code (via pyinstaller) and run it on the Windows 10 machine, it works first the initial time and errors out each additional time. The error I'm getting is a Windows Error 2 (File Not Found).
Traceback (most recent call last):
File "shutil.py", line 544, in move
FileNotFoundError: [WinError 2] The system cannot find the file specified: '\\\\0.0.0.001\\DB\\backup\\DATABASE.bak' -> '\\\\0.0.0.100\\d\\Backups\\DATABASE.bak'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "transfer.py", line 47, in SQL
File "shutil.py", line 558, in move
File "shutil.py", line 257, in copy2
File "shutil.py", line 120, in copyfile
FileNotFoundError: [Errno 2] No such file or directory: '\\\\0.0.0.001\\DB\\backup\\DATABASE.bak'
I'm confused as to why this is. Any guidance would be appreciated so I can stop banging my head on the desk.
I was able to resolve the issue by using os.system() to invoke robocoby then using os.remove() to remove the original file. Works like a charm. Sorry shutil().

How do I set the Engine used by ExecJS to NodeJS?

I'm trying to use the following package: https://github.com/Anorov/cloudflare-scrape/blob/master/cfscrape/init.py
I have the following test file:
import cfscrape
import sys
import execjs
who = sys.argv[1]
scraper = cfscrape.create_scraper()
print scraper.get(who).content
Which is outputting the following error:
Traceback (most recent call last): File "test.py", line 1, in import cfscrape File "build/bdist.macosx-10.10-intel/egg/cfscrape/__init__.py", line 17, in EnvironmentError: Your Javascript runtime 'JavaScriptCore' is not supported due to security concerns. Please use Node.js, V8, or PyV8.
Can anyone point me in the right direction to change my JS_ENGINE from JavaScriptCore to NodeJs

cannot import name SharePointSite

I've got the same error message both on my desktop and on my Windows 2008 R2 server -
Here's the code -
from sharepoint import SharePointSite, basic_auth_opener
server_url = "http://sharepoint/"
site_url = server_url + "path/to/page/Forms/AllItems.aspx"
opener = basic_auth_opener(server_url, "acct", "password")
site = SharePointSite(site_url, opener)
for sp_list in site.lists:
print sp_list.id, sp_list.meta['Title']
When running it I get the following error -
Traceback (most recent call last):
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
ImportError: cannot import name SharePointSite
What's going on? The package is in the location -
C:\Python27\Lib\site-packages\sharepoint
I can import other packages just fine. for example, lxml works fine.
from lxml import etree
no problems.
You named your script sharepoint.py and that masks the library:
Traceback (most recent call last):
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
ImportError: cannot import name SharePointSite
Look at the filenames in the traceback, you can see that the script ends up importing itself; when Python starts your script it loads it as __main__, so importing sharepoint loads your own file one more time, at which point it fails to import itself again.
Rename your script to something else.

error - pypyodbc.win_create_mdb inside multi-levels folder

>>> pypyodbc.win_create_mdb('E:/Database/Japan/201112.mdb')
Its working fine for double-levels but there is an error when I create tripper-level folders error. Would you shade me a light> Thanks.
MS Access library for python
>>> import pypyodbc
>>> pypyodbc.win_create_mdb('E:/Database/Japan/JLeague/201112.mdb')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda\lib\site-packages\pypyodbc-1.3.1-py2.7.egg\pypyodbc.py", line 2715, in win_create_mdb
raise Exception('Failed to create Access mdb file - "%s". Please check file path, permission and Access driver readiness.' %mdb_path)
Exception: Failed to create Access mdb file - "E:/Database/Japan/JLeague/201112.mdb". Please check file path, permission and Access driver readiness.
win_create_mdb will not automatically create a directory if it does not already exist, so you need to check that and possibly create the directory yourself before trying to create the database file within it. Try something like this
# -*- coding: utf-8 -*-
import os
import pypyodbc
directory = 'E:/Database/Japan/J League/'
if not os.path.exists(directory):
os.makedirs(directory)
pypyodbc.win_create_mdb('"' + directory + '201112.mdb' + '"')

BDB Python Interface Error when Reading BDB

bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- /dbs/supermodels.db: unexpected file type or format')
Is this error a result of incompatible BDB versions (1.85 or 3+)? If so, how do I check the versions, trouble-shoot and solve this error?
Yes, this certainly could be due to older versions of the db file, but it would help if you posted the code that generated this exception and the full traceback.
In the absence of this, are you sure that the database file that you're opening is of the correct type? For example, attempting to open a btree file as if it is a hash raises the exception that you are seeing:
>>> import bsddb
>>> bt = bsddb.btopen('bt')
>>> bt.close()
>>> bsddb.hashopen('bt')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/bsddb/__init__.py", line 298, in hashopen
d.open(file, db.DB_HASH, flags, mode)
bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- ./bt: unexpected file type or format')
In *nix you can usually determine the type of db by using the file command, e.g.
$ file /etc/aliases.db cert8.db
/etc/aliases.db: Berkeley DB (Hash, version 8, native byte-order)
cert8.db: Berkeley DB 1.85 (Hash, version 2, native byte-order)
Opening a 1.85 version file fails with the same exception:
>>> db = bsddb.hashopen('/etc/aliases.db') # works, but...
>>> db = bsddb.hashopen('cert8.db')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/bsddb/__init__.py", line 298, in hashopen
d.open(file, db.DB_HASH, flags, mode)
bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- ./cert8.db: unexpected file type or format')
If you need to migrate the database files, you should look at the db_dump, db_dump185 and db_load utilities that come with the bdb distribuition.

Categories

Resources