创建一个python虚拟环境

时间:2020-05-17 12:35:08

标签: python virtualenv

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

我想使用virtualenv创建一个新环境。 我以为// handle your ajax function var ajaxMethod = function() { $.post("/api/newbook", function(data) { // call your function callbackMethod(); }); } // handle your callback function var callbackMethod = function() { } $("#addsell").click(function(e) { // ...other codes // call your ajax function on button click ajaxMehtod(); } 将创建一个新的虚拟环境,就像我的基本安装一样,在这个环境中,我可以安装更多软件包。

看来我想念它是如何工作的。

在我的基本安装中,我可以很好地导入“ virtualenv c:\proj\myNewEnv”。 运行“ TensorFlow之后,我得到了一个名为“ virtualenv c:\proj\myNewEnv”的新文件夹,并且在其中,我有带有Python.exe和activate.bat的目录“脚本”。

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

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

是否可以创建一个虚拟环境,该虚拟环境是我的原始副本的副本,或者取决于我的原始安装(请记住,我没有Internet连接)?

谢谢。

2 个答案:

答案 0 :(得分:0)

运行时您会看到一些有趣的选项

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-packagesGive[s] the virtual environment access to the global site-packages.

试一下。

答案 1 :(得分:0)

我在这里写了一个很长的答案: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

希望这会有所帮助