python:安装anaconda后,如何导入pandas

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32565302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:48:49  来源:igfitidea点击:

python: after installing anaconda, how to import pandas

pythonpandasanaconda

提问by Kanika Singhal

I have installed anaconda. Now when i am trying to run

我已经安装了蟒蛇。现在当我试图跑步时

import pandas as pd

I am getting the following error

我收到以下错误

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pandasFile
ImportError: No module named pandasFile

It is my first day to python. I cannot figure out how to fix it. I am hoping that I have to change some path somewhere. I know it can be a silly question to post here.

这是我接触蟒蛇的第一天。我不知道如何解决它。我希望我必须在某处改变一些路径。我知道在这里发帖可能是一个愚蠢的问题。

采纳答案by Kanika Singhal

If you are facing same problem as mine. Here is the solution which works for me.

如果你遇到和我一样的问题。这是对我有用的解决方案。

  1. Uninstall every python and anaconda.
  2. Download anaconda from here "http://continuum.io/downloads" and only install it (no other python is needed).
  3. Open spyder and import.
  4. If you get any error, type in command prompt

    pip install module_name

  1. 卸载所有 python 和 anaconda。
  2. 从这里“ http://continuum.io/downloads”下载 anaconda并只安装它(不需要其他 python)。
  3. 打开spyder并导入。
  4. 如果出现任何错误,请输入命令提示符

    pip 安装模块名称

I hope it will work for you too

我希望它也对你有用

回答by Alexander

You should first create a new environment in conda. From the terminal, type:

您应该首先在 conda 中创建一个新环境。从终端输入:

$ conda create --name my_env pandas ipython

Python will be installed automatically as part of this installation. After selecting [y] to confirm, you now need to activate this environment:

Python 将作为此安装的一部分自动安装。选择[y]确认后,现在需要激活这个环境:

$ source activate my_env

On Windows I believe it is just:

在 Windows 上,我相信它只是:

$ activate my_env

Now, confirm installed packages:

现在,确认已安装的软件包:

$ conda list

Finally, start python and run your session.

最后,启动 python 并运行您的会话。

$ ipython

回答by m00am

The cool thing about anaconda is, that you can manage virtual environments for several projects. Those also have the benefit of keeping several python installations apart. This could be a problem when several installations of a module or package are interfering with each other.

anaconda 很酷的一点是,您可以管理多个项目的虚拟环境。那些还具有将多个 python 安装分开的好处。当模块或包的多个安装相互干扰时,这可能是一个问题。

Try the following:

请尝试以下操作:

  1. Create a new anaconda environment with user@machine:~$ conda create -n pandas_env python=2.7
  2. Activate the environment with user@machine:~$ source activate pandas_envon Linux/OSX or $ activate pandas_envon Windows. On Linux the active environment is shown in parenthesis in front of the user name in the shell. (I am not sure how windows handles this, but you can see it by typing $ conda info -e. The one with the * next to it is the active one)
  3. Type (pandas_env)user@machine:~$ conda listto show a list of all installed modules.
  4. If pandas is missing from this list, install it (while still inside the pandas_env environment) with (pandas_env)user@machine:~$ conda install pandas, as @Fiabetto suggested.
  5. Open python (pandas_env)user@machine:~$ pythonand try to load pandas again.
  1. 创建一个新的 anaconda 环境 user@machine:~$ conda create -n pandas_env python=2.7
  2. user@machine:~$ source activate pandas_env在 Linux/OSX 或$ activate pandas_envWindows上激活环境。在 Linux 上,活动环境显示在 shell 中用户名前面的括号中。(我不确定 windows 是如何处理这个的,但你可以通过输入 看到它$ conda info -e。旁边带有 * 的那个是活动的)
  3. 键入(pandas_env)user@machine:~$ conda list以显示所有已安装模块的列表。
  4. 如果此列表中缺少 pandas,请(pandas_env)user@machine:~$ conda install pandas按照@Fiabetto 的建议使用 安装它(仍在 pandas_env 环境中)。
  5. 打开python(pandas_env)user@machine:~$ python并再次尝试加载pandas。

Note that now you are working in a python environment, that only knows the modules installed inside the pandas_envenvironment. Every time you want to use it you have to activate the environment. This might feel a little bit clunky at first, but really shines once you have to manage different versions of python (like 2.7 or 3.4) or you need a specific version of a module (like numpy 1.7).

请注意,现在您在 python 环境中工作,它只知道安装在pandas_env环境中的模块。每次要使用它时,都必须激活环境。一开始这可能会让人觉得有点笨拙,但是一旦您必须管理不同版本的 python(如 2.7 或 3.4)或者您需要特定版本的模块(如 numpy 1.7),它就会变得非常出色。

Edit:

编辑:

If this still does not work you have several options:

如果这仍然不起作用,您有几种选择:

  1. Check if the right pandas module is found:

    `(pandas_env)user@machine:~$ python`
    Python 2.7.10 |Continuum Analytics, Inc.| (default, Sep 15 2015, 14:50:01)
    >>> import imp
    >>> imp.find_module("pandas")
    (None, '/path/to/miniconda3/envs/foo/lib/python2.7/site-packages/pandas', ('', '', 5))
    
    # See what this returns on your system.
    
  2. Reinstall pandas in your environment with $ conda install -f pandas. This might help if you files have been corrupted somehow.

  3. Install pandas from a different source (using pip). To do this, create a new environment like above (make sure to pick a different name to avoid clashes here) but replace point 4 by (pandas_env)user@machine:~$ pip install pandas.
  4. Reinstall anaconda (make sure you pick the right version 32bit / 64bit depending on your OS, this can sometimes lead to problems). It could be possible, that your 'normal' and your anaconda python are clashing. As a last resort you could try to uninstall your 'normal' python before you reinstall anaconda.
  1. 检查是否找到了正确的 Pandas 模块:

    `(pandas_env)user@machine:~$ python`
    Python 2.7.10 |Continuum Analytics, Inc.| (default, Sep 15 2015, 14:50:01)
    >>> import imp
    >>> imp.find_module("pandas")
    (None, '/path/to/miniconda3/envs/foo/lib/python2.7/site-packages/pandas', ('', '', 5))
    
    # See what this returns on your system.
    
  2. 使用$ conda install -f pandas. 如果您的文件以某种方式损坏,这可能会有所帮助。

  3. 安装来自不同来源的 Pandas(使用pip)。为此,请创建一个类似上述的新环境(确保选择不同的名称以避免此处发生冲突),但将第 4 点替换为(pandas_env)user@machine:~$ pip install pandas.
  4. 重新安装 anaconda(确保根据您的操作系统选择正确的 32 位/64 位版本,这有时会导致问题)。有可能你的“正常”和你的蟒蛇蟒蛇发生冲突。作为最后的手段,您可以在重新安装 anaconda 之前尝试卸载“普通”python。

回答by a3.14_Infinity

  1. Another alternative is to use Pycharm IDE. For each project, you can set the Project Interpreter in Settings.

  2. For example, if anaconda is installed in /home/user/anaconda2/bin/python, you can select the Project Interpreter and set to this folder.

  3. Since the whole project is set to Anaconda's path, you can import any module which is packaged within Anaconda.

  1. 另一种选择是使用 Pycharm IDE。对于每个项目,您可以在设置中设置项目解释器。

  2. 比如anaconda安装在/home/user/anaconda2/bin/python,可以选择Project Interpreter,设置到这个文件夹。

  3. 由于整个项目都设置为 Anaconda 的路径,因此您可以导入任何打包在 Anaconda 中的模块。

回答by cbrevik

For OSX:

对于 OSX:

I had installed this via Anaconda, and had a hell of a time getting it to work. What helped was adding the Anaconda bin ANDpkgs folder to my PATH.

我已经通过 Anaconda 安装了它,并且花了很长时间让它工作。有帮助的是将 Anaconda binpkgs 文件夹添加到我的路径中。

Since I use fishshell, I did it in my ~/.config/fish/config.fishfile like this:

由于我使用鱼壳,我在我的~/.config/fish/config.fish文件中这样做:

set -g -x PATH $PATH /Users/cbrevik/anaconda/bin /Users/cbrevik/anaconda/pkgs

If you use fishshell like me, this answerwill probably save you some trouble later using pandas as well.

如果你像我一样使用鱼壳,这个答案可能也会为你以后使用熊猫省去一些麻烦。

回答by Lasse Hamborg

I'm using python 3.4 and Anaconda3 4.2.

我正在使用 python 3.4 和 Anaconda3 4.2。

I had the same problem, but it worked (the import pandasworks now anyway) for me to install pandas with pip by writing:

我遇到了同样的问题,但它工作(import pandas无论如何现在都在工作)让我通过编写使用 pip 安装熊猫:

python -m pip install pandas

python -m pip install pandas

Good luck!

祝你好运!

回答by Joe Huang

I've had the same exact problem in that I installed Anaconda because a python script I want to use relies on pandas, and that after so doing, python still returned the same comment that "pandas module is missing" or something to that effect.

我在安装 Anaconda 时遇到了同样的问题,因为我想使用的 Python 脚本依赖于 Pandas,这样做之后,python 仍然返回相同的注释,即“缺少 Pandas 模块”或类似的内容。

When I typed "python" to see which python was being called, I found it was still accessing the older version of python 2.7, even though when I installed Anaconda the installer asked (and I agreed) that it would make its python the default python on my machine (PC running Windows 7).

当我输入“python”以查看正在调用哪个 python 时,我发现它仍在访问旧版本的 python 2.7,即使当我安装 Anaconda 时,安装程​​序询问(并且我同意)它将使其 python 成为默认 python在我的机器上(运行 Windows 7 的 PC)。

I tried to find if there is a CONFIG.SYS file on the PC, but gave up after searching in various places (If anyone knows, please tell me). I got around the problem by writing a one-line batch script named python2.bat that called the Anaconda2 version of python, which then worked. However, it would clearly be better to change the CONFIG.SYS or whatever the PC uses to decide which version of python to call.

我试图在PC上查找是否有CONFIG.SYS文件,但在各个地方搜索后都放弃了(如果有人知道,请告诉我)。我通过编写一个名为 python2.bat 的单行批处理脚本来解决这个问题,该脚本调用了 Anaconda2 版本的 python,然后运行。但是,更改 CONFIG.SYS 或 PC 用来决定调用哪个版本的 python 的任何内容显然会更好。

回答by Taylor

I know there are a lot of answers to this already but I would like to put in my two cents. When creating a virtual environment in anaconda launcher you still need to install the packages you need. This is deceiving because I assumed since I was using anaconda that packages such as pandas, numpy etc would be include. This is not the case. It gives you a fresh environment with none of those packages installed, at least mine did. All my packages installed into the environment with no problem and work correctly.

我知道已经有很多答案了,但我想投入我的两分钱。在 anaconda 启动器中创建虚拟环境时,您仍然需要安装所需的软件包。这是欺骗性的,因为我认为自从我使用 anaconda 以来,会包含诸如 pandas、numpy 等包。不是这种情况。它为您提供了一个没有安装任何软件包的全新环境,至少我的安装了。我所有的软件包都安装到环境中,没有问题并且可以正常工作。

回答by Jose Manuel Gomez Alvarez

You can only import a library which has been installed in your environment.

您只能导入已安装在您的环境中的库。

If you have created a new environment, e.g. to run an older version of Python, maybe you lack 'pandas' package, which is in the 'base' environment of Anaconda by default.

如果您创建了一个新环境,例如运行旧版本的 Python,可能您缺少 'pandas' 包,默认情况下它位于 Anaconda 的 'base' 环境中。

Fix through GUI

通过 GUI 修复

To add it to your environment, from the GUI, select your environment, select "All" in the dropdown list, type pandas in the text field, select the pandas package and Apply.

要将其添加到您的环境中,请从 GUI 中选择您的环境,在下拉列表中选择“全部”,在文本字段中键入 pandas,选择 pandas 包并应用。

Afterwards, select 'Installed' to verify that the package has been correctly installed.

然后,选择“已安装”以验证软件包是否已正确安装。

回答by Korky Kathman

What worked for me, on my Mac at least, was that I opened PyCharm system preferences, then chose my project on the left side. I clicked on Program Interpreter and looked in the list to see that pandas was not installed. I simply chose it from the list on the right (using the search at the top). I clicked the install package and this resolved the issue.

至少在我的 Mac 上,对我有用的是我打开 PyCharm 系统首选项,然后在左侧选择我的项目。我单击 Program Interpreter 并查看列表以查看未安装 Pandas。我只是从右侧的列表中选择了它(使用顶部的搜索)。我点击了安装包,这解决了问题。