简体   繁体   中英

Python virtual environment `venv` cannot find `sqlite3` module

I have created my virtual environment for a project:

python -m venv my_virtual_environment
cd my_project\Scripts
activate.bat

When the following code is executed:

import sys
print('\n Virtual environment: \n', sys.prefix)

import sqlite3

I get the following message:

Virtual environment: 

my_path\my_project\my_virtual_environment

Traceback (most recent call last)

File ...
    import sqlite3

File ...
    from sqlite3.dbapi2 import *

File ...
    from _sqlite3 import *

ImportError: DLL load failed: The specified module could not be found.

According to I read it is not necessary to import sqlite3 .

I tried with virtualenv , as in the link, but I can't activate it.

If I create the virtual environment with Anaconda Navigator the code is executed correctly, but it creates the virtual environment directory in a subdirectory of Anaconda and not in the same directory of the project. This makes me wonder if when I create an executable with pyinstaller it will work.

Edited 16 March 2020

Working specifications:

Windows

  • Edition: Windows 10 Enterprise
  • Version: 1803
  • OS build: 17134.286

Python 3.7.6

CPU architecture

  • Intel Core i7-8700K CPU 3.70 GHz
  • RAM 32 GB
  • 64 bits

You will need to download the sqlite3 DLL and add it in the DLL's folder within your virtual environmnet. For using pyinstaller to package with the sqlite3, this might help you as I faced a similar issue .

The _sqlite3 import works through conda at runtime because it can access the _sqlite3.pyd file but for pyinstaller it needs the DLL that may be missing in your environment.

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