Reputation: 2086
Using Windows 10, and Python 3.4.3, with Powershell, and a clean venv:
PS C:\Users\nward\MyEnvDirectory .\Scripts\Activate.ps1
(MyScriptVenv) PS C:\Users\nward\MyEnvDirectory pip install Package
[...] Success!
I can verify that C:\Users\nward\MyEnvDirectory\Lib\site-packages\Package
exists and looks good.
(MyScriptVenv) PS C:\Users\nward\MyEnvDirectory python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Package
>>>
So, that works. However, if I try to run the same line, e.g "import Package" from within a .py file in the same directory, like so -
(MyScriptEnv) PS C:\Users\nward\MyEnvDirectory .\main.py
- then I get an ImportError: No module named 'Package'
.
Based on what I've read, this is usually due to a PATH error, shadowing, or mixing versions of Python. python --version
gives Python 3.4.3
, and I don't believe I have any other versions installed. I don't think this is due to shadowing since my script file and the package have different names. So it must be due to a PATH problem, but I don't understand what it is, since the interpreter clearly has no problems with the PATH! This question Installed Python script cannot import package modules seems to be exactly my problem but it was due to shadowing/directory structure issues. I've also read that this could be a permissions issue (e.g an elevated interpreter but a low-permission executing shell), but the permissions in this case are OK.
Anyone have any suggestions for where I should look next? This is a really basic issue and I'm sure I'm just misunderstanding something, but I'm a little stumped. Thanks!
Upvotes: 1
Views: 691
Reputation: 90899
Seems like when you run open python interactive interpreter inside the virtual environment using -
PS C:\Users\nward\MyEnvDirectory python
You are getting the python installation from the virtualenv, but when you run your script as -
.\main.py
It is picking up the python outside the virtualenv, since when you directly run the file, windows picks up the python executable that is registered for .py
files. You can try running the script as -
python main.py
From within the virtualenv.
Upvotes: 1