gpt4 book ai didi

python - 在 MS-Windows 机器上使用 Python virtualenv

转载 作者:可可西里 更新时间:2023-11-01 09:32:07 25 4
gpt4 key购买 nike

我接手了一个同事在共享的 MS-Windows 笔记本电脑上创建的项目。该项目是用 PyCharm 编写的,我的同事为其创建了一个 virtualenv

命令提示符中运行代码时,virtualenvsite-packages,即使virtualenv 已激活,无法访问。我尝试了一下,发现如果我在 venv\Scripts 中,那么 virtualenv 会按预期工作。

那么,问题是 - 如何在 MS-Windows 机器上为 virtualenv 设置事件工作目录/路径?

(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 的结果,而此目录被“挂载”在与当前所在位置不同的位置。)

关于python - 在 MS-Windows 机器上使用 Python virtualenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com