Note: This is not an issue with Visual Studio, but rather with incompatible dll versions. The steps below replicate the problem since running in Visual Studio in debug mode breaks on exceptions being thrown. If you just run, the thrown exception is handled elsewhere and the program works fine. But since I spend a lot of time in debug mode, I would prefer to fix this problem.
When debugging, I want to be able to step into modules I have added to my Virtual Environment in Visual Studio. I get a 'library not found' error that I am not able to fix. Here are the steps:
- In Visual Studio create a new Python Application.
- Create a virtual environment for that application (Python 3.6 64 bit).
pip install twilio
into your virtual environment. You get the following output.
...
----- Installing 'twilio' -----
Collecting twilio
Using cached twilio-6.10.5-py2.py3-none-any.whl
Collecting pytz (from twilio)
Using cached pytz-2018.3-py2.py3-none-any.whl
Collecting six (from twilio)
Using cached six-1.11.0-py2.py3-none-any.whl
Collecting PyJWT>=1.4.2 (from twilio)
Using cached PyJWT-1.6.0-py2.py3-none-any.whl
Collecting requests>=2.0.0; python_version >= "3.0" (from twilio)
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting pysocks; python_version >= "3.0" (from twilio)
Using cached PySocks-1.6.8.tar.gz
Collecting certifi>=2017.4.17 (from requests>=2.0.0; python_version >= "3.0"->twilio)
Using cached certifi-2018.1.18-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests>=2.0.0; python_version >= "3.0"->twilio)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests>=2.0.0; python_version >= "3.0"->twilio)
Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests>=2.0.0; python_version >= "3.0"->twilio)
Using cached idna-2.6-py2.py3-none-any.whl
Installing collected packages: pytz, six, PyJWT, certifi, chardet, urllib3, idna, requests, pysocks, twilio
Running setup.py install for pysocks: started
Running setup.py install for pysocks: finished with status 'done'
Successfully installed PyJWT-1.6.0 certifi-2018.1.18 chardet-3.0.4 idna-2.6 pysocks-1.6.8 pytz-2018.3 requests-2.18.4 six-1.11.0 twilio-6.10.5 urllib3-1.22
----- Successfully installed 'twilio' -----
Add the following line to the top of your .py file:
from twilio.rest import Client
In Visual Studio go to tools > options > python > debugging. Make sure 'Enable debugging of Python standard library' is checked
Run the application. You get the following error:
ModuleNotFoundError: No module named 'OpenSSL'
pip install pyopenssl
You get the following output:
...
----- Installing 'pyopenssl' -----
Collecting pyopenssl
Using cached pyOpenSSL-17.5.0-py2.py3-none-any.whl
Requirement already satisfied: six>=1.5.2 in c:\users\x\source\repos\pythonapplication9\pythonapplication9\env\lib\site-packages (from pyopenssl)
Collecting cryptography>=2.1.4 (from pyopenssl)
Using cached cryptography-2.1.4-cp36-cp36m-win_amd64.whl
Requirement already satisfied: idna>=2.1 in c:\users\x\source\repos\pythonapplication9\pythonapplication9\env\lib\site-packages (from cryptography>=2.1.4->pyopenssl)
Collecting cffi>=1.7; platform_python_implementation != "PyPy" (from cryptography>=2.1.4->pyopenssl)
Using cached cffi-1.11.5-cp36-cp36m-win_amd64.whl
Collecting asn1crypto>=0.21.0 (from cryptography>=2.1.4->pyopenssl)
Using cached asn1crypto-0.24.0-py2.py3-none-any.whl
Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography>=2.1.4->pyopenssl)
Using cached pycparser-2.18.tar.gz
Installing collected packages: pycparser, cffi, asn1crypto, cryptography, pyopenssl
Running setup.py install for pycparser: started
Running setup.py install for pycparser: finished with status 'done'
Successfully installed asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.1.4 pycparser-2.18 pyopenssl-17.5.0
----- Successfully installed 'pyopenssl' -----
Run the application. You get the following error:
asn1crypto._ffi.LibraryNotFoundError: The library libcrypto could not be found
The error is thrown in the file named _big_num_ctypes.py
in asn1crypto
. The code line where this is thrown is:
libcrypto_path = find_library(b'crypto' if sys.version_info < (3,) else 'crypto')
if not libcrypto_path:
raise LibraryNotFoundError('The library libcrypto could not be found')
Update: I was asked to provide the full backtrace. I modified the code in this way to print it:
import unittest
import traceback
class Test_test1(unittest.TestCase):
def test_A(self):
try:
from twilio.rest import Client
except Exception as e:
print('foo')
foo = traceback.extract_stack()
traceback.print_exc(e)
if __name__ == '__main__':
unittest.main()
As before the import line throws the exception but the exception is not caught and the lines in 'except' clause are never executed from twilio.rest import Client
update 2: I somehow had gotten this to work following @Prateek and @user8212173. But now it is not working again. As both suggested, the problem is that crypto.dll is not there. So I went thru the steps below to add it with no success:
- I installed Win64 OpenSSL v1.1.0j from https://slproweb.com/products/Win32OpenSSL.html (pointed to from https://wiki.openssl.org/index.php/Binaries). It does not contain crypto.dll.
- I then installed crypto.dll from http://www.dlldownloader.com/crypto-dll/ (as @user8212173 suggested) (there is only a 32 bit version) and followed the instructions. I then got a new error message "ImportError: DLL load failed: %1 is not a valid Win32 application" which means that the crypto.dll I installed has a version conflict (I am running 64bit python on a 64bit computer). I remember installing it from Unofficial Windows Binaries for Python Extension Packages I can't find it there. So where do I get a working 64bit version of crypto.dll?