简体   繁体   English

从 Windows 命令提示符复制 Anaconda 环境

[英]Replicating Anaconda Environment From Windows Command Prompt

I want to run Python code from a Windows command prompt.我想从 Windows 命令提示符运行 Python 代码。 Some of the features in scripts that I run in an Anaconda environment, ie from Anaconda Prompt, Spyder, or Jupyter Notebook, are not available when I run the same script from a Windows command prompt.当我从 Windows 命令提示符运行同一脚本时,我在 Anaconda 环境(即来自 Anaconda Prompt、Spyder 或 Jupyter Notebook)中运行的脚本中的某些功能不可用。 For instance, the read.csv method in Pandas can read a 2.8 GB tab-delimited text file when the script is run in an Anaconda environment, but I get the following message when the same script is run from a Windows command prompt within the environment where I've installed a copy of Python: "pandas.errors.ParserError: Error tokenizing data. C error: out of memory".例如,当脚本在 Anaconda 环境中运行时,Pandas 中的 read.csv 方法可以读取 2.8 GB 制表符分隔的文本文件,但是当在该环境中从 Windows 命令提示符运行相同的脚本时,我会收到以下消息我在其中安装了 Python 的副本:“pandas.errors.ParserError:错误标记数据。C 错误:内存不足”。 I assume the Anaconda environment has a package installed for reading large text files.我假设 Anaconda 环境安装了一个用于读取大文本文件的包。 I've tried running the script from my home directory, where Anaconda stores my Python scripts by default, and I still receive the same "out of memory" message.我已经尝试从我的主目录运行脚本,默认情况下 Anaconda 存储我的 Python 脚本,但我仍然收到相同的“内存不足”消息。 I've also run the script from C:[my home directory]\AppData\Local\Continuum\anaconda3, where Anaconda is apparently installed, and I experience the same memory problem.我还从 C:[my home directory]\AppData\Local\Continuum\anaconda3 运行脚本,显然安装了 Anaconda,但我遇到了同样的内存问题。 Does anybody know how I can run a Python script in the same environment that Anaconda Prompt, Spyder, and Jupyter Notebooks use?有人知道如何在 Anaconda Prompt、Spyder 和 Jupyter Notebooks 使用的相同环境中运行 Python 脚本吗?

This might be resulting from using different Python interpreters.这可能是由于使用不同的 Python 解释器造成的。 For example, the 32-bit version of Python in windows only gets 2GB of memory to use.例如,Windows 中的 32 位版本的 Python 只能使用2GB 的内存 There's a chance that you might be using that on the command line vs a 64-bit interpreter in your notebook.您可能会在命令行上使用它,而不是在笔记本中使用 64 位解释器。

Compare the environments using the sys package.使用sys包比较环境。 From the command line, try the following command.从命令行尝试以下命令。

python -c "from __future__ import print_function; import sys; print(sys.version); print(sys.executable)"

I shared an example below.我在下面分享了一个例子。 In this case, I'm running these commands from inside Anaconda Prompt, which comes installed with (all?) versions of Anaconda.在这种情况下,我从 Anaconda Prompt 内部运行这些命令,它随(所有?)版本的 Anaconda 一起安装。

(base) C:\Users\my.user.name>python -c "from __future__ import print_function; import sys; print(sys.version); print(sys.executable)"
3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]
C:\ProgramData\Anaconda3\python.exe

(base) C:\Users\my.user.name>conda env list
# conda environments:
#
base                  *  C:\ProgramData\Anaconda3
colors                   C:\Users\my.user.name\.conda\envs\colors
someotherenv             C:\Users\my.user.name\.conda\envs\someotherenv
hellostevep              C:\Users\my.user.name\.conda\envs\hellostevep

(base) C:\Users\my.user.name>conda activate colors

(colors) C:\Users\my.user.name>python -c "from __future__ import print_function; import sys; print(sys.version); print(sys.executable)"
3.7.2 (default, Feb 21 2019, 17:35:59) [MSC v.1915 64 bit (AMD64)]
C:\Users\my.user.name\.conda\envs\colors\python.exe

Notice that python.exe is different depending on my environment.请注意, python.exe因我的环境而异。 You may have additional Python environments on your OS that are separate from Anaconda.您的操作系统上可能有与 Anaconda 不同的其他 Python 环境。 For example, you may have a Python environment setup for Windows PowerShell.例如,您可能为 Windows PowerShell 设置了 Python 环境。 If so, you could run the command python -c "from __future__ import print_function; import sys; print(sys.version); print(sys.executable)" from your PowerShell prompt as well and then compare the output.如果是这样,您也可以在 PowerShell 提示符下运行命令python -c "from __future__ import print_function; import sys; print(sys.version); print(sys.executable)"然后比较输出。

Inside of the Jupyter Notebook where the Pandas method is working, try running similar code inside a cell (example below).在运行 Pandas 方法的 Jupyter Notebook 中,尝试在一个单元格中运行类似的代码(如下例)。

import sys
print(sys.version)
print(sys.executable)

Do you get the same output?你得到相同的输出吗?

If the output is identical between your notebook and the command line, this may not be the issue you're experiencing.如果您的笔记本和命令行之间的输出相同,这可能不是您遇到的问题。 If so, could you share the code that you're executing from inside the notebook as well as the code from the script that you're running on the command line?如果是这样,您能否分享您从笔记本内部执行的代码以及您在命令行上运行的脚本的代码?

I have Miniconda but I guess my solution can be adapted also to Anaconda.我有 Miniconda,但我想我的解决方案也可以适用于 Anaconda。 From Windows command prompt I run从 Windows 命令提示符我运行

"C:\Users\<username>\Miniconda3\Scripts\activate.bat"  environment_to_use

and I get a working Conda environment.我得到了一个工作的 Conda 环境。 This way I was also able to run a python script with drag and drop.这样我还可以通过拖放运行 python 脚本。 I created a link to the real python script needing a certain Conda environment and I changed the Destination property of the link to我创建了一个指向需要特定 Conda 环境的真实 python 脚本的链接,并将链接的Destination属性更改为

"C:\Users\<username>\Miniconda3\Scripts\activate.bat" environment_to_use  &&  "C:\Users\<username>\Miniconda3\envs\<environment_to_use>\python.exe"  "<path>\my_python_script.py"

This way if I drop any files on the link to the python script, the python script runs using dropped files as arguments.这样,如果我在指向 python 脚本的链接上放置任何文件,python 脚本将使用放置的文件作为参数运行。

暂无
暂无

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

相关问题 从 VBA(或命令行)启动 Anaconda 提示 - Launching Anaconda Prompt from VBA (Or Command Line) 如何通过在Windows中选择anaconda或Python从命令提示符运行Python文件? - How to run Python file from command prompt by selecting anaconda or Python in windows? exe文件在Anaconda提示window但在Windows命令行不工作 - exe file works in Anaconda Prompt window but not in Windows command line Python 模块请求在 anaconda 提示符下工作但不在 Windows 10 命令行中工作 - Python module requests working in anaconda prompt but not in windows 10 command line 如何从 windows 命令提示符中杀死 python? - how to kill python from windows command prompt? Python:从python脚本的Windows命令提示符中执行windows命令 - Python: Executing the windows command in windows command prompt from python script Windows 命令提示导入错误:无法导入所需的依赖项:Z2EA9510C37F7F89E4941 - Python Pandas in Windows Command Prompt ImportError: Unable to import required dependencies: numpy (Anaconda) Jupyter Notebook 在 Anaconda Prompt for Qiskit 中命令失败 - Jupyter Notebook failed command in Anaconda Prompt for Qiskit 对于Anaconda 3.2019版本的anaconda命令提示符,访问被拒绝错误 - Access is denied error for anaconda command prompt for Anaconda 3.2019 version 如何在python解释器中启用vim风格的命令行编辑在cygwin环境中安装anaconda windows - How to enable vim style command line editing in python interpreter in anaconda windows install in cygwin environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM