在 MS-Windows 机器上使用 Python virtualenv
Using a Python virtualenv on a MS-Windows machine
我接手了一个同事在共享 MS-Windows 笔记本电脑上创建的项目。该项目是用 PyCharm 编写的,我的同事为其创建了一个 virtualenv
。
当 运行 Command Prompt
virtualenv
的 site-packages
中的代码时,即使 virtualenv
已激活,也无法访问。我试了一下,发现如果我在 venv\Scripts
中,那么 virtualenv
会按预期工作。
所以,问题是 - 如何在 MS-Windows 机器上为 virtualenv
设置活动工作 directory/path?
(venv) C:\Users\George\Documents\Simulation>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages', 'C:\Python27\lib\site-packages\win32', 'C:\Python27\lib\site-packages\win32\lib', 'C:\Python27\lib\site-packages\Pythonwin']
对
(venv) C:\Users\George\Documents\Simulation\venv\Scripts>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Users\George\Documents\Simulation\venv\DLLs', 'C:\Users\George\Documents\Simulation\venv\lib', 'C:\Users\George\Documents\Simulation\venv\lib\plat-win', 'C:\Users\George\Documents\Simulation\venv\lib\lib-tk', 'C:\Users\George\Documents\Simulation\venv\Scripts', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\Users\George\Documents\Simulation\venv', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages\win32', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages\win32\lib', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages\Pythonwin']
还有:
(venv) C:\Users\George\Documents\Simulation\venv\Scripts>where python
C:\Users\George\Documents\Simulation\venv\Scripts\python.exe
C:\Python27\python.exe
(venv) C:\Users\George\Documents\Simulation>where python
C:\Python27\python.exe
和路径:
(venv) C:\Users\George\Documents\Simulation>path
PATH=C:\Users\general\Desktop\shared\Simulation\venv\Scripts;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Users\George\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\bin;
(venv) C:\Users\George\Documents\Simulation\venv\Scripts>path
PATH=C:\Users\general\Desktop\shared\Simulation\venv\Scripts;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Users\George\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\bin;
您的venv/scripts/activate.cmd
可能不正确,添加
C:\Users\general\Desktop\shared\Simulation\venv\Scripts\
到您的 path
环境变量而不是
C:\Users\George\Documents\Simulation\venv\Scripts\
编辑 activate.cmd
并相应地修复以 set path=...
开头的行。
(这可能是创建 virtualenv 的结果,而此目录 "mounted" 位于与当前所在位置不同的位置。)
我接手了一个同事在共享 MS-Windows 笔记本电脑上创建的项目。该项目是用 PyCharm 编写的,我的同事为其创建了一个 virtualenv
。
当 运行 Command Prompt
virtualenv
的 site-packages
中的代码时,即使 virtualenv
已激活,也无法访问。我试了一下,发现如果我在 venv\Scripts
中,那么 virtualenv
会按预期工作。
所以,问题是 - 如何在 MS-Windows 机器上为 virtualenv
设置活动工作 directory/path?
(venv) C:\Users\George\Documents\Simulation>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages', 'C:\Python27\lib\site-packages\win32', 'C:\Python27\lib\site-packages\win32\lib', 'C:\Python27\lib\site-packages\Pythonwin']
对
(venv) C:\Users\George\Documents\Simulation\venv\Scripts>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Users\George\Documents\Simulation\venv\DLLs', 'C:\Users\George\Documents\Simulation\venv\lib', 'C:\Users\George\Documents\Simulation\venv\lib\plat-win', 'C:\Users\George\Documents\Simulation\venv\lib\lib-tk', 'C:\Users\George\Documents\Simulation\venv\Scripts', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\Users\George\Documents\Simulation\venv', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages\win32', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages\win32\lib', 'C:\Users\George\Documents\Simulation\venv\lib\site-packages\Pythonwin']
还有:
(venv) C:\Users\George\Documents\Simulation\venv\Scripts>where python
C:\Users\George\Documents\Simulation\venv\Scripts\python.exe
C:\Python27\python.exe
(venv) C:\Users\George\Documents\Simulation>where python
C:\Python27\python.exe
和路径:
(venv) C:\Users\George\Documents\Simulation>path
PATH=C:\Users\general\Desktop\shared\Simulation\venv\Scripts;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Users\George\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\bin;
(venv) C:\Users\George\Documents\Simulation\venv\Scripts>path
PATH=C:\Users\general\Desktop\shared\Simulation\venv\Scripts;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Users\George\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\bin;
您的venv/scripts/activate.cmd
可能不正确,添加
C:\Users\general\Desktop\shared\Simulation\venv\Scripts\
到您的 path
环境变量而不是
C:\Users\George\Documents\Simulation\venv\Scripts\
编辑 activate.cmd
并相应地修复以 set path=...
开头的行。
(这可能是创建 virtualenv 的结果,而此目录 "mounted" 位于与当前所在位置不同的位置。)