Everything below runs in virtual environment. I have a script that performs an import to the netifaces module. the package appears in pip list When I run it in interactive mode, it does the import without problems, but when I run it as a script, it fails.
Simple version of the script:
import socket # Import socket module
import netifaces
print("Hello")
Capturing messages in the terminal:
Package Version
--------------------------------- -------
backports.entry-points-selectable 1.1.1
distlib 0.3.3
filelock 3.3.2
ipaddress 1.0.23
netaddr 0.8.0
netifaces 0.11.0
pip 21.3.1
platformdirs 2.4.0
pyasn1 0.4.8
pysmb 1.2.7
setuptools 58.5.3
virtualenv 20.10.0
wheel 0.36.2
(venv) C:\Temp_C_10\Proyectos\PruebasSMB>py
Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> import netifaces
>>> print("hello")
hello
>>> quit()
(venv) C:\Temp_C_10\Proyectos\PruebasSMB>py test.py
Traceback (most recent call last):
File "C:\Temp_C_10\Proyectos\PruebasSMB\test.py", line 10, in <module>
import netifaces
ModuleNotFoundError: No module named 'netifaces'
(venv) C:\Temp_C_10\Proyectos\PruebasSMB>
What am I doing wrong? What am I missing? What don't I just understand?
I edit and add information, as recommended:
It seems that I have found the cause, although I do not fully understand it. If anyone can give me more information I would appreciate it.
I have added to the program, which prints sys.path. Launching the script as py test.py, sys.path doesn't pick up the virtual environment, which it does when launching it as python test.py.