简体   繁体   中英

python virtualenv can not access file in home directory

I am using this code in Django settings to construct the DB path

from os.path import expanduser
defautl_db_path = expanduser("~")+"/db.sqlite3"

The code runs well if I installed Django system wide but if I run the application inside virtualenv the application throws an exception saying unable to open database file .

How can I solve this?

did you check permissions on the home directory of the process owner? Is it true, that you are using a Unix OS?

Most likely the django user is www-data.

If you checked this, you could try to address home directory of a specific user by adding username to ~.

from os.path import expanduser
defautl_db_path = expanduser("~www-data")+"/db.sqlite3"

In documentation is written:

os.path.expanduser(path) On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user's home directory.

On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user's home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM