繁体   English   中英

创建 python 虚拟环境

[英]Creating a python virtual env

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

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

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

在我的基本安装中,我有“ TensorFlow ”导入就好了。 运行“ virtualenv c:\proj\myNewEnv ”后,我得到了一个名为“ c:\proj\myNewEnv ”的新文件夹,在其中,我有目录“脚本”与Z23EEEB79EEB4347BDD7555FCDDD4347BDD26BDDA

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

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

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

提前致谢。

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

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. .

试一试。

我在这里写了一个长答案: python 虚拟环境是否避免冗余安装?

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

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

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

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

此外,当您激活虚拟环境时,每当您执行 python 文件时,这将调用该环境的解释器(如果有),其中包含您安装的所有依赖项。

因此,在您的情况下,请确保首先激活 venv,安装您的依赖项,然后执行您的文件

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

conda list --export > requirements.txt 

接着:

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

请检查此问题以获得进一步解释: pip freeze 和 conda list 之间的区别

希望这会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM