【问题标题】:Creating a python virtual env创建一个 python 虚拟环境
【发布时间】:2020-09-03 04:55:55
【问题描述】:

我在没有互联网连接的电脑上安装了带有 Python 3.6.4 的 Anaconda。

我想使用 virtualenv 创建一个新环境。 我认为virtualenv c:\proj\myNewEnv 将创建一个新的虚拟环境,就像我的基本安装一样,在这个环境中我可以安装更多的包。

我似乎想念它是如何工作的。

在我的基础安装中,“TensorFlow”的导入很好。 运行“virtualenv c:\proj\myNewEnv”后,我得到了一个名为“c:\proj\myNewEnv”的新文件夹,在其中,我有包含 python.exe 和 activate.bat 的目录“Scripts”。

但是无论我运行什么,我得到的都是一个不知道 tensorflow 的 python shell。

它似乎只是我的python.exe和pip.exe的副本,没有所有原始包。

有没有办法创建一个虚拟环境,它是我的原始副本或依赖于我的原始安装(请记住,我没有互联网连接)?

提前致谢。

【问题讨论】:

    标签: python virtualenv


    【解决方案1】:

    运行时可以看到一些有趣的选项

    python -m virtualenv --help
    
    Usage: virtualenv.py [OPTIONS] DEST_DIR
    
    Options:
      --version             show program's version number and exit
      -h, --help            show this help message and exit
      -v, --verbose         Increase verbosity.
      -q, --quiet           Decrease verbosity.
      -p PYTHON_EXE, --python=PYTHON_EXE
                            The Python interpreter to use, e.g.,
                            --python=python3.5 will use the python3.5 interpreter
                            to create the new environment.  The default is the
                            interpreter that virtualenv was installed with
                            (/usr/sbin/python)
      --clear               Clear out the non-root install and start from scratch.
      --no-site-packages    DEPRECATED. Retained only for backward compatibility.
                            Not having access to global site-packages is now the
                            default behavior.
      --system-site-packages
                            Give the virtual environment access to the global
                            site-packages.
      --always-copy         Always copy files rather than symlinking.
      --relocatable         Make an EXISTING virtualenv environment relocatable.
                            This fixes up scripts and makes all .pth files
                            relative.
      --no-setuptools       Do not install setuptools in the new virtualenv.
      --no-pip              Do not install pip in the new virtualenv.
      --no-wheel            Do not install wheel in the new virtualenv.
      --extra-search-dir=DIR
                            Directory to look for setuptools/pip distributions in.
                            This option can be used multiple times.
      --download            Download pre-installed packages from PyPI.
      --no-download, --never-download
                            Do not download pre-installed packages from PyPI.
      --prompt=PROMPT       Provides an alternative prompt prefix for this
                            environment.
      --setuptools          DEPRECATED. Retained only for backward compatibility.
                            This option has no effect.
      --distribute          DEPRECATED. Retained only for backward compatibility.
                            This option has no effect.
      --unzip-setuptools    DEPRECATED.  Retained only for backward compatibility.
                            This option has no effect.
    

    其中的首领是--system-site-packages,其中Give[s] the virtual environment access to the global site-packages.

    试一试。

    【讨论】:

      【解决方案2】:

      我在这里写了一个很长的答案:Does a python virtual environment avoid redundant installs?

      总之,您可以在基本虚拟环境中使用pip freeze > requirements.txt 命令,然后通过执行pip install requirements.txt 安装新命令。

      如果您希望 tenserflow 和一个环境中的所有依赖项都可以用于另一个环境:pip freezepip install 命令 是你的朋友吗

      现在请记住:当您激活虚拟环境时,您会在提示符的开头看到(venv)(或您给它起的任何名称)。

      这意味着您将安装的每个软件包(例如pip install numpy)都可用于此特定环境。如果您停用并切换到另一个 venv,则需要重新安装它。

      另外,当你激活一个虚拟环境时,每当你执行一个 python 文件时,它都会调用这个环境的解释器(如果有的话),其中包含你安装的所有依赖项。

      所以在你的情况下,确保你首先激活 venv,安装你的依赖,然后执行你的文件

      最后一件事:如果您使用 Anaconda...您可以使用 Anaconda 特定命令执行相同的操作:

      conda list --export > requirements.txt 
      

      然后:

      conda create --name <envname> --file requirements.txt
      

      请查看这个问题以获得进一步的解释: Difference between pip freeze and conda list

      希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 2014-01-24
        • 2016-03-29
        • 2021-07-30
        • 2017-03-14
        • 1970-01-01
        相关资源
        最近更新 更多