簡體   English   中英

導入 Python 包時出錯(例如 Numpy)

[英]Error with Import Python Packages(Such as Numpy)

我正在使用計算集群,但無權訪問整個集群。 因此,我正在嘗試在本地(在我的“主”目錄中)安裝 python 包,但是我在從腳本導入它們時遇到了問題。

我試圖更新我的 PATH 和我的 PYTHONPATH,將兩者都設置為 ~/.local/lib。 我正在導入的集群上已經創建了一個 python 3.7.3 模塊。 但是我無法訪問它以向其添加更多軟件包,這就是我必須在本地安裝的原因。 模塊的路徑和我的 .local 目錄都在 PATH 中。

當我使用 pip 安裝軟件包時,出現此錯誤:“錯誤:由於環境錯誤無法安裝軟件包:[Errno 13] 權限被拒絕:'/s1/opt/python-3.7.3/lib/python3.7/ site-packages/numpy' 考慮使用--user選項或檢查權限。” 因此,我必須在本地安裝它。 當我使用--user選項時,一切似乎都安裝得很好。 另外,我的.local目錄下的python3.7版本只有python和site-packages目錄,而集群模塊中的python3.7還有很多其他文件。

安裝這些軟件包后,當我轉到腳本並嘗試運行它們時,出現這些錯誤。

Traceback (most recent call last):
  File "fragment_assignment.py", line 10, in <module>
    import numpy as np
  File "/s1/snagaraj/.local/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/s1/snagaraj/.local/lib/python3.7/site-packages/numpy/core/__init__.py", line 71, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using /usr/bin/python),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

     Note: this error has many possible causes, so please don't comment on
     an existing issue about this - open a new one instead.

原來的錯誤是:

沒有名為 _multiarray_umath 的模塊

作為旁注,向其他人提出我發現的錯誤的所有修復對我都不起作用。

我建議為您的應用程序/開發需求創建一個虛擬環境,然后在其中運行。 通常,虛擬環境是確保您擁有所需的所有依賴項並且不會與其他事物產生大量沖突問題的好方法。 可能最簡單的方法是使用pipenv 另一篇關於虛擬環境和 pipenv 的文章

首先,確保安裝了pipenv

$ pip install --user pipenv

為您的項目創建一個文件夾並將目錄更改為其中(或直接cd到您當前的項目目錄):

$ mkdir my_project
$ cd my_project

然后開始從my_project目錄中安裝您需要的軟件包:

$ pipenv install numpy scipy pandas

或任何你需要的包。 該操作完成后,您可以通過運行以下命令來激活您的環境:

$ pipenv shell

然后你想用python做什么。 或者,您可以在環境中運行一個腳本(我們稱之為my_script.py ):

$ pipenv run python my_script.py

看起來某處可能缺少依賴項。 嘗試安裝libatlas3-base以獲取該文件。

sudo apt-get install libatlas3-base

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM