本文介绍了Python3.7 venv不会创建虚拟环境目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法在Windows中创建虚拟环境:

I'm trying to create a virtual environment in windows using:

python3 -m venv <dir-name>

运行上述命令后,当我在cmd中检查当前目录的内容时,看不到venv目录.该命令不会引发任何错误.

When I check the contents of the current directory in cmd after running the above command, I don't see the venv directory show up. The command doesn't throw any errors.

这个人似乎有同样的问题: Python venv未创建虚拟环境

This person seems to have had the same problem:Python venv not creating virtual environment

但是可以接受的答案是重新安装python,这对我不起作用.其他答案建议改为安装virtualenv,但据我所知,它与venv不同,它建议用于python3.3 +

But the accepted answer was to re-install python, which didn't work for me. Other answers suggest installing virtualenv instead, but as far as I know that is different from venv, which is recommended for python3.3+

有人知道如何用venv解决此问题吗?我已经尝试了python 3.6和3.7

Does anyone know how to solve this issue with venv? I've tried python 3.6 and 3.7

问题似乎是未使用PATH中设置的python.exe的位置.而是使用:C:\Users\GSI\AppData\Local\Microsoft\WindowsApps\python3.exe.我不确定如何修复我的环境变量.需要重启吗?我有很多东西在运行,但如有必要,我可以重新启动

The problem seems to be that the location of python.exe set in PATH is not being used. It is instead using: C:\Users\GSI\AppData\Local\Microsoft\WindowsApps\python3.exe. I'm not sure how to fix my environment variable. Is a restart required? I have quite a few things running but I can restart if it's necessary

Edit2 :我被要求发布以下命令输出的屏幕截图.如您所见,仅使用"python3"运行它们时没有输出.当我使用安装exe的完整路径运行命令时,我确实得到了ouptut:

I was asked to post a screenshot of the output of the following commands. As you can see, there is no output when I run them with just "python3". When I run the commands with the full path where the exe is installed, I do get ouptut:

我在这里找到了有用的信息: https://superuser.com/问题/1437590/typing-python-on-windows-10-version-1903-command-prompt-opens-microsoft-stor

Edit 3: I found a helpful post here:https://superuser.com/questions/1437590/typing-python-on-windows-10-version-1903-command-prompt-opens-microsoft-stor

当您没有安装python或将python添加到PATH变量中时,显然在CMD中键入"python"会打开Microsoft存储并在C:\Users\GSI\AppData\Local\Microsoft\WindowsApps中创建一个python.exe文件.我猜我是在初次安装python时但在添加PATH变量之前尝试执行python代码.

Apparently typing "python" into CMD when you don't have python installed/added to the PATH variable opens up the Microsoft store and creates a python.exe file in C:\Users\GSI\AppData\Local\Microsoft\WindowsApps. I'm guessing I tried executing python code when I first installed python but before I had added the PATH variable.

我按照帖子中的说明删除了python.exe和python3.exe的应用程序执行别名".这摆脱了WindowsApps中的exe文件(我无法手动将其删除).

I followed the instructions in the post to remove the "Application Execution Aliases" for python.exe and python3.exe. That got rid of the exe files in WindowsApps (I wasn't able to manually delete them).

但是,现在当我在CMD中键入where python3时,我得到:INFO: Could not find files for the given pattern(s).

However, now when I type where python3 in CMD, I get:INFO: Could not find files for the given pattern(s).

似乎没有选择我的PATH值.我尝试重新启动计算机,但是没有运气

It seems like it's not picking up my PATH values. I tried restarting my computer but no luck

推荐答案

它应该可以工作,我对其进行了多次测试( eg :).
是的,它们是2种不同动物:

It should work, I tested it several times (e.g.: [SO]: PyWin32 (226) and virtual environments).
And yes, they are 2 different kinds of animals:

  • [Python 3.Docs]: venv - Creation of virtual environment
  • [PyPA.VirtualEnv]: Virtualenv.

示例:

e:\Work\Dev\StackOverflow\q059885771>sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> set PY
PYTHONPATH=e:\Work\Dev\Utils\current

[prompt]> dir /b

[prompt]> "c:\Install\pc064\Python\Python\03.07.06\python.exe" -c "import sys, venv;print(sys.version);print(venv)"
3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]
<module 'venv' from 'c:\\Install\\pc064\\Python\\Python\\03.07.06\\lib\\venv\\__init__.py'>

[prompt]> "c:\Install\pc064\Python\Python\03.07.06\python.exe" -m venv
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip] [--prompt PROMPT]
            ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR

[prompt]> time<nul
The current time is: 21:51:57.12
Enter the new time:
[prompt]> "c:\Install\pc064\Python\Python\03.07.06\python.exe" -m venv ".\venv_dir"

[prompt]> echo %errorlevel%
0

[prompt]> time<nul
The current time is: 21:52:10.54
Enter the new time:
[prompt]>
[prompt]> dir /b
venv_dir

[prompt]> "venv_dir\Scripts\python.exe"
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z


[prompt]>

注意事项(注意事项):

  • 确保事物确实是您认为的样子:使用完整路径(就像我对 Python 可执行文件所做的那样).要查看您所用的解释器,请在控制台中键入 where python3
    • 也可以将其应用于环境目录
    • Make sure that things are actually what you think they are: use full paths (like I did for the Python executable). To see what interpreter is invoked in your case, type where python3 in the console
      • Might also apply this to the environment directory

      通过所有上述检查后,您应该能够创建和使用( venv 创建的)虚拟环境.

      When all the above checks pass, you should be able to create and use a (venv created) virtual environment.

      我查看了问题中添加的 URL :(提及@ Update,@ TusharGautam,@ MattJecha,@ Ramhound很好的答案,其中包含有用的信息和资源.

      I took a look at the URL added in the question: [SuperUser]: Typing "python" on Windows 10 (version 1903) command prompt opens Microsoft store (mentioning @Update, @TusharGautam, @MattJecha, @Ramhound great answers, containing useful info and resources).

      显然,这是因为 App执行别名 中有2个与 App Installer 相关的项目.然后,我走得更远,还从 Microsoft Store 安装了 Python 3.7 .
      两者( App Installer Python 3.7 )都将一些项目(包括可执行文件)放在" %USERPROFILE%\ AppData \ Local \ Microsoft \ WindowsApps ".有趣的是,它们的大小为 0 个字节,并且它们既不是快捷方式也不是符号链接.我倾向于认为它们实际上并不存在于文件系统中,而是 Win 所显示和表现的某种模拟项",好像它们是真实的( MS Windows资源管理器中将 .NET程序集(基本上是 .dll 文件)显示为目录时执行了操作.

      Apparently, it is because of the 2 App Installer related items from App execution aliases. Then I went further, and also installed Python 3.7 from Microsoft Store.
      Both (App Installer and Python 3.7) place some items (including executables) in "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps". The funny thing is that they are 0 bytes sized, and they are neither shortcuts nor symlinks. I tend to think they don't actually exist on the filesystem, but are some kind of "mock entries" that Win displays and acts as if they would be real (the same thing MS did when displaying the .NET assemblies (which are basically .dll files) as directories in Windows Explorer).

      在下图中,我选择了2个框式项目作为示例:

      In the image below, I chose the 2 boxed items as examples:

      选中一个时,另一个将自动取消选中,这很有意义,因为它们都指向同一个可执行文件"( python3.exe ).

      When checking one, the other gets automatically unchecked, which makes sense as both point to the same "executable" (python3.exe).

      [prompt]> :: Python3.7 (python3.exe - installed from Microsoft Store) checked
      [prompt]> dir "c:\Users\cfati\AppData\Local\Microsoft\WindowsApps\py*"
       Volume in drive C is SSD0-WIN
       Volume Serial Number is F2CE-FA29
      
       Directory of c:\Users\cfati\AppData\Local\Microsoft\WindowsApps
      
      20/01/24  12:32                 0 python3.exe
      20/01/24  12:12    <DIR>          PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
                     1 File(s)              0 bytes
                     1 Dir(s)  197,737,488,384 bytes free
      
      [prompt]> python3 -c "import sys, os;print(\"VER: {0:}\nEXE: {1:}\nCWD: {2:}\nPyPATH: {3:}\".format(sys.version, sys.executable, os.getcwd(), sys.path))"
      VER: 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 01:54:44) [MSC v.1916 64 bit (AMD64)]
      EXE: C:\Users\cfati\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe
      CWD: e:\Work\Dev\StackOverflow\q059885771
      PyPATH: ['', 'e:\\Work\\Dev\\Utils\\current', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\python37.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\lib', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\lib\\site-packages']
      
      [prompt]> echo %errorlevel%
      0
      
      [prompt]>
      [prompt]> :: App Installer (python3.exe) checked
      [prompt]> dir "c:\Users\cfati\AppData\Local\Microsoft\WindowsApps\py*"
       Volume in drive C is SSD0-WIN
       Volume Serial Number is F2CE-FA29
      
       Directory of c:\Users\cfati\AppData\Local\Microsoft\WindowsApps
      
      20/01/24  12:35                 0 python3.exe
      20/01/24  12:12    <DIR>          PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
                     1 File(s)              0 bytes
                     1 Dir(s)  197,737,291,776 bytes free
      
      [prompt]> python3 -c "import sys, os;print(\"VER: {0:}\nEXE: {1:}\nCWD: {2:}\nPyPATH: {3:}\".format(sys.version, sys.executable, os.getcwd(), sys.path))"
      Access is denied.
      
      [prompt]> :: !!! "This app can't run on your PC" popped up !!!
      [prompt]> echo %errorlevel%
      5
      

      请注意,可执行文件(" C:\ Users \ cfati \ AppData \ Local \ Microsoft \ WindowsApps \ PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0 \ python.exe ")也是一个模拟文件"条目",真正的条目实际上位于" %ProgramFiles%\ WindowsApps "

      As a note, the executable ("C:\Users\cfati\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe") is also a "mock entry", the real one is actually located in "%ProgramFiles%\WindowsApps"

      来自 [MS.DevBlogs]:谁将Python放入Windows 10 May 2019更新中?(重点是我的):

      因此,您可以找到 python3 实际上不是 python3 (当然,它以 python 的形式出色地).有趣的是,在您的情况下,它有效",而在我看来,它抱怨( ERROR_ACCESS_DENIED ).

      So, there you go, python3 is not actually python3 (of course, appplies to python as well). The funny thing is that in your case it "worked", while in mine it complained (ERROR_ACCESS_DENIED).

      结论:

      • 我必须坚持始终使用完整路径.这样一来,您就不会受到 OS 中发生的更改的影响(嗯,您的文件可能会被删除或损坏,但是您会遇到更大的问题)
      • 每次指定完整路径都会很快变得很烦人,这就是为什么应该/应该使用 PATH 环境变量的原因.当然,这可能在 90 %% 的情况下很好地工作(并显着改善了体验),但是有些情况(例如,当其他已安装的 app s/工具时)同时提供了 Python 的代码,将其 dir 添加到 PATH 中),可以触发有趣的结果
      • I must insist on always using the full paths. That way you are not impacted by changes happening in the OS (well, your files might get deleted or corrupted, but then you'd have bigger issues)
      • Specifying the full path every time, would soon become very annoying, that's why the PATH environment variable could / should be used. Of course, this works fine (and significantly improves the experience) in probably 90+% of the cases, but there are some (for example when other installed apps / tools that also ship Python, add their dirs into PATH) where it can trigger funny results

      这篇关于Python3.7 venv不会创建虚拟环境目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 14:48
查看更多