欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python虚拟环境virtualenv

程序员文章站 2022-06-06 15:51:08
...

win10安装python最新版本,如果忘记勾选添加到path,需要手动配置:
C:\Users\Administrator\AppData\Local\Programs\Python\Python38
C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Scripts
python虚拟环境virtualenv
安装时遇到2503报错:
https://blog.csdn.net/netsec_steven/article/details/52637088
python虚拟环境virtualenv

设置pip清华镜像:
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/

PS C:\Windows\system32> pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U --user

如果pip升级报错:

ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-uninstall-m77510sy\\pip.exe'
Consider using the `--user` option or check the permissions.

需要添加–user选项赋予权限:
https://www.cnblogs.com/CSGO-416482145/p/12589995.html

以上是安装python环境时的问题。下面是安装virtualenv。

最新版本的python自带venv功能,但通过pip安装virtualenv仍可行。
在Python3.4之前,virtualenv需要独立手动安装后,方可执行。之后的版本中,Python自带了一个venv库,执行Python –m venv就可以达到与virtalenv同样的效果。更多的用户习惯上还是使用virtualenv的方式。
https://www.liujiangblog.com/course/python/6

创建虚拟环境:

PS E:\py> virtualenv cms0929
virtualenv -p 
PS E:\py> C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe E:\py\cms2

-p: 指定你要虚拟的Python版本,这里选择了本地的python2.7
(就是python.exe的地址)

–-no-site-packages:表示在建立虚拟环境时不将原版本中的第三方库拷贝过来,这样就能获得一个纯净的Python环境。
(从virtualenv 版本20开始,默认就是’–no-site-packages‘了。这个参数作废了https://www.cnblogs.com/johnthegreat/p/13138495.html)

d:\Python_virtualenvs\for_django:表明在该目录下,建立一个叫做for_django的虚拟环境,这样的命名方式,让你一眼就能看出这个虚拟环境是为谁建立的。
(创建虚拟环境的目标地址)
创建完的目录:

python虚拟环境virtualenv
我仔细看了一下这两个目录,内容一模一样,所以加不加-p参数对于我这台电脑中只有一个python3.8主环境的情况来说都一样。
python虚拟环境virtualenv

**虚拟环境的命令(linux):

source venv/bin/activate

win10下不需要加source:

PS E:\py> .\cms2\Scripts\activate
(cms2) PS E:\py> PWD

注意不是执行activate.bat

但是前提是需要执行:

PS E:\py> Set-ExecutionPolicy -ExecutionPolicy UNRESTRICTED

原因:win10默认安全策略下,power shell不能运行脚本
https://blog.csdn.net/u011583025/article/details/99704809

参考:
https://blog.csdn.net/qq_41621362/article/details/94793693

注意区分哪些是命令,哪些是目录:
比如:venv\Scripts\activate中,venv\Scripts\是相对路径,activate是脚本文件,不同版本不同系统(linux)下的路径不同:
win10:
virtualenv venv
venv\Scripts\activate(相对路径)
venv是我们主动创建的目录,可以修改未其他名称:
virtualenv cms1234
cms1234\Scripts\activate

Linux(https://www.liaoxuefeng.com/wiki/1016959663602400/1019273143120480):
virtualenv –no-site-packages venv
source venv/bin/activate
这种写法不符合win10,也不符合我的virtualenv版本。

整个屏幕打印:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Windows\system32> pip

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  cache                       Inspect and manage pip's wheel cache.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to
                              WARNING, ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --no-input                  Disable prompting for input.
  --proxy <proxy>             Specify a proxy in the form [user:aaa@qq.com]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
                              (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted, even though it does not have valid or any
                              HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the
                              certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output
  --no-python-version-warning
                              Silence deprecation warnings for upcoming unsupported Pythons.
  --use-feature <feature>     Enable new functionality, that may be backward incompatible.
  --use-deprecated <feature>  Enable deprecated functionality, that will be removed in the future.
PS C:\Windows\system32> pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4e/5f/528232275f6509b1fff703c9280e58951a81abe24640905de621c9f81839/pip-20.2.3-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 595 kB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.2.1
    Uninstalling pip-20.2.1:
      Successfully uninstalled pip-20.2.1
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-uninstall-m77510sy\\pip.exe'
Consider using the `--user` option or check the permissions.

PS C:\Windows\system32> pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U --user
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already up-to-date: pip in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (20.2.3)
PS C:\Windows\system32> pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Writing to C:\Users\Administrator\AppData\Roaming\pip\pip.ini
PS C:\Windows\system32> pip install --upgrade virtualenv
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting virtualenv
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/12/51/36c685ff2c1b2f7b4b5db29f3153159102ae0e0adaff3a26fd1448232e06/virtualenv-20.0.31-py2.py3-none-any.whl (4.9 MB)
     |████████████████████████████████| 4.9 MB 1.7 MB/s
Collecting appdirs<2,>=1.4.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3b/00/2344469e2084fb287c2e0b57b72910309874c3245463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting six<2,>=1.9.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting filelock<4,>=3.0.0
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting distlib<1,>=0.3.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f5/0a/490fa011d699bb5a5f3a0cf57de82237f52a6db9d40f33c53b2736c9a1f9/distlib-0.3.1-py2.py3-none-any.whl (335 kB)
     |████████████████████████████████| 335 kB 3.2 MB/s
Installing collected packages: appdirs, six, filelock, distlib, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 six-1.15.0 virtualenv-20.0.31
PS C:\Windows\system32> virtualenv
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py] [--creator {builtin,cpython3-win,venv}] [--seeder {app-data,pip}] [--no-seed]
                  [--activators comma_sep_list] [--clear] [--system-site-packages] [--symlinks | --copies] [--no-download | --download] [--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip]
                  [--no-setuptools] [--no-wheel] [--no-periodic-update] [--symlink-app-data] [--prompt prompt] [-h]
                  dest
virtualenv: error: the following arguments are required: dest
SystemExit: 2
PS C:\Windows\system32> virtualenv --version
virtualenv 20.0.31 from c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages\virtualenv\__init__.py
PS C:\Windows\system32> E:
PS E:\> ls


    目录: E:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2019/11/5     10:22                .android
d-----        2017/12/1     16:38                Bitnami
d-----        2020/7/10      9:13                CloudMusic
d-----        2019/5/30     16:03                cygwin64
d-----         2019/7/2     12:07                DRIVERS
d-----       2018/12/13     21:54                Foxmail 7.2
d-----       2019/10/17     17:46                inetpub
d-----        2018/6/18      9:47                Intel
d-----        2019/1/15     13:31                legacy
d-----        2015/9/29     14:53                mfg
d-r---        2020/4/22     12:53                Program Files
d-r---        2020/4/22     12:43                Program Files (x86)
d-----        2018/6/17     20:42                SnapPlugin
d-----       2014/11/14      5:25                support
d-----       2015/11/24     11:00                SWTOOLS
d-----        2016/7/20      3:41                Tencent
d-----         2016/2/2     19:39                tomcat
d-r---       2019/10/17     19:04                Users
d-----        2020/5/13     17:11                Windows
d-----        2020/9/11     21:52                wx
d-----        2019/6/10     13:19                备份20160726
d-----        2020/9/28      9:31                迅雷下载
------        2016/2/19     17:12         131072 9384.img
------        2016/2/19     17:12           1024 Reserve.img


PS E:\> cd wx
PS E:\wx> ls


    目录: E:\wx


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2020/9/11     21:42                dMicroMsg
d-----        2020/9/11     20:06                pMicroMsg
-a----        2020/9/11     21:28     2751180353 微信(com.tencent.mm).bak


PS E:\wx> cd ..
PS E:\> ls


    目录: E:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2019/11/5     10:22                .android
d-----        2017/12/1     16:38                Bitnami
d-----        2020/7/10      9:13                CloudMusic
d-----        2019/5/30     16:03                cygwin64
d-----         2019/7/2     12:07                DRIVERS
d-----       2018/12/13     21:54                Foxmail 7.2
d-----       2019/10/17     17:46                inetpub
d-----        2018/6/18      9:47                Intel
d-----        2019/1/15     13:31                legacy
d-----        2015/9/29     14:53                mfg
d-r---        2020/4/22     12:53                Program Files
d-r---        2020/4/22     12:43                Program Files (x86)
d-----        2018/6/17     20:42                SnapPlugin
d-----       2014/11/14      5:25                support
d-----       2015/11/24     11:00                SWTOOLS
d-----        2016/7/20      3:41                Tencent
d-----         2016/2/2     19:39                tomcat
d-r---       2019/10/17     19:04                Users
d-----        2020/5/13     17:11                Windows
d-----        2020/9/11     21:52                wx
d-----        2019/6/10     13:19                备份20160726
d-----        2020/9/28      9:31                迅雷下载
------        2016/2/19     17:12         131072 9384.img
------        2016/2/19     17:12           1024 Reserve.img


PS E:\> cd py
PS E:\py> ls
PS E:\py> virtualenv cms0929
created virtual environment CPython3.8.6.final.0-64 in 2484ms
  creator CPython3Windows(dest=E:\py\cms0929, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==20.2.2, setuptools==49.6.0, wheel==0.35.1
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
PS E:\py> python3.6 -m
python3.6 : 无法将“python3.6”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确
保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ python3.6 -m
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (python3.6:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS E:\py> virtualenv -p
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py]
virtualenv: error: argument -p/--python: expected one argument
SystemExit: 2
PS E:\py> virtualenv -p C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe --no-site-package
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py] [--creator {builtin,cpython3-win,venv}] [--seeder {app-data,pip}] [--no-seed]
                  [--activators comma_sep_list] [--clear] [--system-site-packages] [--symlinks | --copies] [--no-download | --download] [--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip]
                  [--no-setuptools] [--no-wheel] [--no-periodic-update] [--symlink-app-data] [--prompt prompt] [-h]
                  dest
virtualenv: error: the following arguments are required: dest
SystemExit: 2
PS E:\py> virtualenv -p C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe --no-site-package E:\py\cms2
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py] [--creator {builtin,cpython3-win,venv}] [--seeder {app-data,pip}] [--no-seed]
                  [--activators comma_sep_list] [--clear] [--system-site-packages] [--symlinks | --copies] [--no-download | --download] [--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip]
                  [--no-setuptools] [--no-wheel] [--no-periodic-update] [--symlink-app-data] [--prompt prompt] [-h]
                  dest
virtualenv: error: unrecognized arguments: --no-site-package
SystemExit: 2
PS E:\py> virtualenv -p C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe --no-site-packages E:\py\cms2
usage: virtualenv [--version] [--with-traceback] [-v | -q] [--app-data APP_DATA] [--reset-app-data] [--upgrade-embed-wheels] [--discovery {builtin}] [-p py] [--creator {builtin,cpython3-win,venv}] [--seeder {app-data,pip}] [--no-seed]
                  [--activators comma_sep_list] [--clear] [--system-site-packages] [--symlinks | --copies] [--no-download | --download] [--extra-search-dir d [d ...]] [--pip version] [--setuptools version] [--wheel version] [--no-pip]
                  [--no-setuptools] [--no-wheel] [--no-periodic-update] [--symlink-app-data] [--prompt prompt] [-h]
                  dest
virtualenv: error: unrecognized arguments: --no-site-packages
SystemExit: 2
PS E:\py> virtualenv -p C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe E:\py\cms2             created virtual environment CPython3.8.6.final.0-64 in 1534ms
  creator CPython3Windows(dest=E:\py\cms2, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Administrator\AppData\Local\pypa\virtualenv)
    added seed packages: pip==20.2.2, setuptools==49.6.0, wheel==0.35.1
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
PS E:\py> source venv/bin/activate
source : 无法将“source”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径
正确,然后再试一次。
所在位置 行:1 字符: 1
+ source venv/bin/activate
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS E:\py> .\cms2\Scripts\activate
.\cms2\Scripts\activate : 无法加载文件 E:\py\cms2\Scripts\activate.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参
阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\cms2\Scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS E:\py> source .\cms2\Scripts\activate
source : 无法将“source”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径
正确,然后再试一次。
所在位置 行:1 字符: 1
+ source .\cms2\Scripts\activate
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (source:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS E:\py> .\cms2\Scripts\activate.bat
PS E:\py> python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
KeyboardInterrupt
>>> ^Z

PS E:\py> python --version
Python 3.8.6
PS E:\py> Set-ExecutionPolicy -ExecutionPolicy UNRESTRICTED

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y](Y)  [A] 全是(A)  [N](N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): a
PS E:\py> Set-ExecutionPolicy -ExecutionPolicy UNRESTRICTED

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y](Y)  [A] 全是(A)  [N](N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): A
PS E:\py> .\cms2\Scripts\activate
(cms2) PS E:\py> PWD

Path
----
E:\py


(cms2) PS E:\py> python --version
Python 3.8.6
(cms2) PS E:\py>