Python file does not recognise library installed in virtual environment

Geesh_SO Source

Explanation

I had a previous post but I deleted it due to it being meandering and coming to the wrong conclusions.

The command I'm running below doesn't make sense on its own, but it's the file that is ultimately causing problems when other scripts I have run this file down the code path.

I have Python 3.5.1 installed, with PIP and virtualenv.

If I activate my virtual environment and run pip list I get the following.

future (0.16.0)
generateDS (2.20a0)
lxml (3.6.4)
mysql-connector (2.1.4)
pefile (2016.3.28)
pip (9.0.1)
PyInstaller (3.2)
pypiwin32 (219)
pypyodbc (1.3.3)
python-dateutil (2.5.3)
setuptools (38.5.1)
six (1.10.0)
wheel (0.30.0)

Now if I run virtualenv_foobar_20180214093433\Scripts\process_includes.py.

It immediately throws this error.

Traceback (most recent call last):
  File "C:\Code\site\Python\FooBar\virtualenv_foobar_20180214093433\Scripts\process_includes.py", line 28, in <module>
    from lxml import etree
ImportError: No module named lxml

The relevant code in that file is this.

from copy import deepcopy
from lxml import etree

try:
    from gds_inner_name_map import Inner_name_map
except ImportError:
    Inner_name_map = None

Question

Why is this happening? I've activated the virtual environment, and pip lists the library that it is looking for.

Extra Information

I have Python 2 and 3 installed, neither are in Windows Path by default.

Placing print(platform.python_version()) inside process_includes.py prints the Python 2 version despite me being inside a virtual environment.

process_includes.py has the Python 3 held inside the virtual environment as its shebang.

Update

I have no idea why despite neither Python 2 or 3 being in the PATH, and being inside a Python 3 virtual environment that running the Python file directly would result in it being handled by Python 2.

In the end I updated my scripts to explicitly reference the virtual environment python.exe which was a bit of a pain but not the end of the world.

pythonpython-3.xpipvirtualenv

Answers

comments powered by Disqus