标签归档:dependencies

scipy.misc模块没有属性读取?

问题:scipy.misc模块没有属性读取?

我正在尝试读取图像。但是,它不接受该scipy.misc.imread零件。这可能是什么原因?

>>> import scipy
>>> scipy.misc
<module 'scipy.misc' from 'C:\Python27\lib\site-packages\scipy\misc\__init__.pyc'>
>>> scipy.misc.imread('test.tif')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    scipy.misc.imread('test.tif')
AttributeError: 'module' object has no attribute 'imread'

I am trying to read an image with scipy. However it does not accept the scipy.misc.imread part. What could be the cause of this?

>>> import scipy
>>> scipy.misc
<module 'scipy.misc' from 'C:\Python27\lib\site-packages\scipy\misc\__init__.pyc'>
>>> scipy.misc.imread('test.tif')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    scipy.misc.imread('test.tif')
AttributeError: 'module' object has no attribute 'imread'

回答 0

您需要安装Pillow(以前称为PIL)。从在文档scipy.misc

请注意,Pillow不是SciPy的依赖项,但是如果没有它,下面列表中指示的图像处理功能将不可用:

imread

安装Pillow后,我可以imread如下访问:

In [1]: import scipy.misc

In [2]: scipy.misc.imread
Out[2]: <function scipy.misc.pilutil.imread>

You need to install Pillow (formerly PIL). From the docs on scipy.misc:

Note that Pillow is not a dependency of SciPy but the image manipulation functions indicated in the list below are not available without it:

imread

After installing Pillow, I was able to access imread as follows:

In [1]: import scipy.misc

In [2]: scipy.misc.imread
Out[2]: <function scipy.misc.pilutil.imread>

回答 1

imread在SciPy 1.0.0中已弃用,在1.2.0中将被删除。使用imageio.imread代替。

import imageio
im = imageio.imread('astronaut.png')
im.shape  # im is a numpy array
(512, 512, 3)
imageio.imwrite('imageio:astronaut-gray.jpg', im[:, :, 0])

imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.

import imageio
im = imageio.imread('astronaut.png')
im.shape  # im is a numpy array
(512, 512, 3)
imageio.imwrite('imageio:astronaut-gray.jpg', im[:, :, 0])

回答 2

版本1.2.0之后,imread贬值!因此,要解决此问题,我必须安装版本1.1.0。

pip install scipy==1.1.0

imread is depreciated after version 1.2.0! So to solve this issue I had to install version 1.1.0.

pip install scipy==1.1.0

回答 3

对于Python 3,最好是使用imreadmatplotlib.pyplot

from matplotlib.pyplot import imread

For Python 3, it is best to use imread in matplotlib.pyplot:

from matplotlib.pyplot import imread

回答 4

如果有人遇到相同的问题,请卸载scipy并安装scipy == 1.1.0

$ pip uninstall scipy

$ pip install scipy==1.1.0

In case anyone encountering the same issue, please uninstall scipy and install scipy==1.1.0

$ pip uninstall scipy

$ pip install scipy==1.1.0

回答 5

您需要Python Imaging Library(PIL),但是but!PIL项目似乎已被放弃。特别是,它尚未移植到Python3。因此,如果要在Python 3中使用PIL功能,则最好使用Pillow,它是PIL的半官方分支,并且正在积极开发中。实际上,如果您完全需要现代的PIL实现,我建议您选择Pillow。就像一样简单pip install pillow。由于它使用与PIL相同的命名空间,因此实质上是直接替代。

这个叉子有多“半官方”?你可能会问。“ 枕头”文档的“ 关于”页面说:

自上次发布PIL之后,随着时间的流逝,新发布PIL的可能性降低。但是,我们尚未听到官方的“ PIL已死”声明。因此,如果您仍然希望支持PIL,请先在此处报告问题,然后在此处打开相应的枕头票。

请提供第一张票证的链接,以便我们可以跟踪上游问题。

但是,PIL 官方站点上的最新PIL版本发布于2009年11月15日。我认为,在将近八年没有新版本发布之时,我们可以肯定地说Pillow是PIL的继承者。因此,即使您不需要Python 3支持,我也建议您避免使用PyPI中可用的古老PIL 1.1.6发行版,而只需安装新的,最新的,兼容的Pillow。

You need the Python Imaging Library (PIL) but alas! the PIL project seems to have been abandoned. In particular, it hasn’t been ported to Python 3. So if you want PIL functionality in Python 3, you’ll do well do use Pillow, which is the semi-official fork of PIL and appears to be actively developed. Actually, if you need a modern PIL implementation at all I’d recommend Pillow. It’s as simple as pip install pillow. As it uses the same namespace as PIL it’s essentially a drop-in replacement.

How “semi-official” is this fork? you may ask. The About page of the Pillow docs say this:

As more time passes since the last PIL release, the likelihood of a new PIL release decreases. However, we’ve yet to hear an official “PIL is dead” announcement. So if you still want to support PIL, please report issues here first, then open corresponding Pillow tickets here.

Please provide a link to the first ticket so we can track the issue(s) upstream.

However, the most recent PIL release on the official PIL site is dated November 15, 2009. I think we can safely proclaim Pillow as the successor of PIL after (as of this writing) nearly eight years of no new releases. So even if you don’t need Python 3 support, I suggest you eschew the ancient PIL 1.1.6 distribution available in PyPI and just install fresh, up-to-date, compatible Pillow.


回答 6

通过以下命令安装枕头库:

pip install pillow

请注意,所选答案已过时。查看SciPy的文档

请注意,Pillow(https://python-pillow.org/)不是SciPy的依赖项,但如果没有它,则下面列表中指示的图像处理功能将不可用。

Install the Pillow library by following commands:

pip install pillow

Note, the selected answer has been outdated. See the docs of SciPy

Note that Pillow (https://python-pillow.org/) is not a dependency of SciPy, but the image manipulation functions indicated in the list below are not available without it.


回答 7

答案是:misc.imread在SciPy 1.0.0中已弃用,在1.2.0中将被删除。imageio是一个选项,它将返回类型为object的对象:

<class 'imageio.core.util.Image'>

但要使用image2,而不要使用imageio

import cv2
im = cv2.imread('astronaut.png')

我将是类型: <class 'numpy.ndarray'>

由于numpy数组的计算速度更快。

As answered misc.imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. imageio is one option,it will return object of type :

<class 'imageio.core.util.Image'>

but instead of imageio, use cv2

import cv2
im = cv2.imread('astronaut.png')

im will be of type : <class 'numpy.ndarray'>

As numpy arrays are faster to compute.


回答 8

Imread使用PIL库,如果已安装该库,则使用:“ from scipy.ndimage import imread”

资料来源:http : //docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.ndimage.imread.html

Imread uses PIL library, if the library is installed use : “from scipy.ndimage import imread”

Source: http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.ndimage.imread.html


回答 9

python -m pip install pillow

这对我有用。

python -m pip install pillow

This worked for me.


回答 10

您需要一个python图像库(PIL),但是现在仅PIL还不够,您最好安装Pillow。这很好。

You need a python image library (PIL), but now PIL only is not enough, you’d better install Pillow. This works well.


回答 11

在Jupyter Notebook中运行以下命令,我收到了类似的错误消息:

from skimage import data
photo_data = misc.imread('C:/Users/ers.jpg')
type(photo_data)

“错误”消息:

D:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ ipykernel_launcher.py:3:DeprecationWarning:已imread弃用!imread在SciPy 1.0.0中已弃用,在1.2.0中将被删除。使用imageio.imread 代替。这与ipykernel软件包分开,因此我们可以避免导入,直到

并使用以下我解决了:

import matplotlib.pyplot
photo_data = matplotlib.pyplot.imread('C:/Users/ers.jpg')
type(photo_data)

Running the following in a Jupyter Notebook, I had a similar error message:

from skimage import data
photo_data = misc.imread('C:/Users/ers.jpg')
type(photo_data)

‘error’ msg:

D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead. This is separate from the ipykernel package so we can avoid doing imports until

And using the following I got it solved:

import matplotlib.pyplot
photo_data = matplotlib.pyplot.imread('C:/Users/ers.jpg')
type(photo_data)

回答 12

我在jupyter笔记本上具有图像提取所需的所有软件包,但即使如此,它仍然显示相同的错误。

Jupyter Notebook上的错误

阅读以上评论,我已经安装了必需的软件包。请告诉我是否错过了一些包裹。

pip3 freeze | grep -i -E "pillow|scipy|scikit-image"
Pillow==5.4.1
scikit-image==0.14.2

scipy==1.2.1

I have all the packages required for the image extraction on jupyter notebook, but even then it shows me the same error.

Error on Jupyter Notebook

Reading the above comments, I have installed the required packages. Please do tell if I have missed some packages.

pip3 freeze | grep -i -E "pillow|scipy|scikit-image"
Pillow==5.4.1
scikit-image==0.14.2

scipy==1.2.1

回答 13

在python 3.6中为我工作的解决方案如下

py -m pip安装枕头

The solution that work for me in python 3.6 is the following

py -m pip install Pillow


如何打包python应用程序使其可点子安装?

问题:如何打包python应用程序使其可点子安装?

我在业余时间正在编写django应用程序,以参加我们正在上班的小费竞赛。我认为我会明智地使用这段时间,并逐步了解virtualenv,pip,打包,django 1.3以及如何编写易于重新分发的应用程序。到目前为止,一切都很好。

我要负责包装部分。例如,GitHub上的许多django应用程序大多(大致)以相同的方式捆绑在一起。我将以django-uni-forms为例。

我所做的一个假设是,MANIFEST.insetup.py是pip完成工作所需的唯一必需的部分。那是对的吗?如果我的假设是错误的,还需要哪些其他组件?

是否通常生成了所需的包装文件,或者是手工制作的?可以描述依赖项然后再安装吗?我的应用程序依赖django-uni-forms,并且在requirements.txt我用来安装依赖项的应用程序中的文件中列出了它;但这是包装系统可以解决的问题吗?

我需要遵循哪些步骤来打包我的应用程序,以使pip能够安装它和任何依赖项?

I’m writing a django application in my spare time for a footy-tipping competition we’re running at work. I figured I’d use this time wisely, and get up to speed on virtualenv, pip, packaging, django 1.3, and how to write an easily redistributable application. So far, so good.

I’m up to the packaging part. A lot of the django apps on GitHub for instance are mostly bundled (roughly) the same way. I’ll use django-uni-forms as an example.

An assumption I’m making is that the MANIFEST.in and setup.py are the only required pieces that pip needs to do its job. Is that correct? What other components are necessary if my assumption is wrong?

Are the required packaging files generally generated, or are they crafted by hand? Can dependencies be described and then installed also? My application depends on django-uni-forms, and I have it listed in a requirements.txt file within my app which I used to install the dependency; but is that something that the packaging system can take care of?

What are the steps I need to follow to package my application in such a way that pip will be able to install it and any dependencies?


回答 0

是的,MANIFEST.in而且setup.py应该是足够的。

这篇博客文章确实具有有关此主题的一些很好的信息: 打包Django可重用应用程序

这是另一个很好的详细概述,对我有很大帮助: Python打包用户指南

尤其是包含静态文件(模板)的提示很重要,因为起初可能并不明显。

是的,您可以在setup.py其中指定所需的软件包,这些软件包在安装应用程序时会自动获取。

例如:

    install_requires = [
        'django-profiles',
        'django-uni-forms',
    ],

显然,现在我们在两个地方定义了依赖项,但这并不一定意味着这些信息是重复的:setup.py与requirements.txt

通过此设置,您的软件包应可通过安装pip


正如Pierre在评论中指出的那样,Django的官方文档中现在还有一个相关的部分: 打包您的应用

然后是这个“完全不完整”的指南,它确实很好地概述了打包并将程序包上载到PyPI:分享您的爱的劳动:PyPI的快速与肮脏

Yes, MANIFEST.in and setup.py should be sufficient.

This blog post really has some good information on this topic: Packaging a Django reusable app

And here’s another good, detailed overview that helped me a lot: Python Packaging User Guide

Especially the tips to get your static files (templates) included are important as this might not be obvious at first.

And yes, you can specify required packages in your setup.py which are automatically fetched when installing your app.

For example:

    install_requires = [
        'django-profiles',
        'django-uni-forms',
    ],

Obviously now we have two places where dependencies are defined, but that doesn’t necessarily mean that these information are duplicated: setup.py vs requirements.txt

With this setup your package should be installable via pip.


As Pierre noted in the comments, there’s now also a relevant section in Django’s official documentation: Packaging your app

And then there is this “completely incomplete” guide, which really gives a great overview over packaging and uploading a package to PyPI: Sharing Your Labor of Love: PyPI Quick And Dirty


自动创建requirements.txt

问题:自动创建requirements.txt

有时我从那里下载python源代码,github却不知道如何安装所有依赖项。如果没有requirements.txt文件,则必须手动创建。问题是:给定python源代码目录,是否可以requirements.txt从import部分自动创建?

Sometimes I download the python source code from github and don’t know how to install all the dependencies. If there is no requirements.txt file I have to create it by hands. The question is: Given the python source code directory is it possible to create requirements.txt automatically from the import section?


回答 0

如果您使用虚拟环境,pip freeze > requirements.txt就可以了。IF NOTpigar将是您不错的选择。

顺便说一句,我不保证它能在2.6下使用。

更新

建议使用Pipenv或其他工具来改善开发流程。

对于Python 3,请在下面使用

pip3 freeze > requirements.txt

If you use virtual environment, pip freeze > requirements.txt just fine. IF NOT, pigar will be a good choice for you.

By the way, I do not ensure it will work with 2.6.

UPDATE:

Pipenv or other tools is recommended for improving your development flow.

For Python 3 use below

pip3 freeze > requirements.txt

回答 1

您可以使用以下代码来生成requirements.txt文件:

pip install pipreqs

pipreqs /path/to/project

与pipreqs相关的更多信息可以在这里找到。

有时您会碰到pip freeze,但这会保存环境中的所有程序包,包括您当前项目中未使用的程序包。

You can use the following code to generate a requirements.txt file:

pip install pipreqs

pipreqs /path/to/project

more info related to pipreqs can be found here.

Sometimes you come across pip freeze, but this saves all packages in the environment including those that you don’t use in your current project.


回答 2

就我而言,我使用的是Anaconda,因此从我的环境中的conda终端运行以下命令即可解决该问题,并自动为我创建了此需求txt文件:

conda list -e > requirements.txt

摘自Github链接pratos / condaenv.txt

如果看到错误,并且您正在使用anaconda,请尝试使用.yml选项:

conda env export > <environment-name>.yml

供其他人使用的环境…或者如果要在其他计算机上创建新环境:conda env create -f .yml

.yml选项在这里找到

In my case, I use Anaconda, so running the following command from conda terminal inside my environment solved it, and created this requirements txt file for me automatically:

conda list -e > requirements.txt

This was taken from this Github link pratos/condaenv.txt

If an error been seen, and you are using anaconda, try to use the .yml option:

conda env export > <environment-name>.yml

For other person to use the environment…Or if you are creating a new enviroment on other machine: conda env create -f .yml

.yml option been found here


回答 3

确保为python3.7运行pip3。

pip3 freeze >> yourfile.txt

在执行上述命令之前,请确保已创建虚拟环境。

python3:

pip3 install virtualenv
python3 -m venv <myenvname> 

python2

pip install virtualenv
virtualenv <myenvname>

之后,将您的源代码放在目录中。如果您现在运行python文件,则可能在使用非本机模块时无法启动。您可以安装那些正在运行的模块

pip3 install <module> or pip install <module> 

除了您所在的环境,这不会影响您的整个模块列表。

现在,您可以在顶部执行命令,现在有了一个需求文件,其中仅包含您在虚拟环境中安装的模块。现在,您可以在顶部运行命令。

我建议大家使用环境,因为这样的事情会使事情变得容易。

希望这会有所帮助。

Make sure to run pip3 for python3.7.

pip3 freeze >> yourfile.txt

Before executing the above command make sure you have created a virtual environment.

python3:

pip3 install virtualenv
python3 -m venv <myenvname> 

python2:

pip install virtualenv
virtualenv <myenvname>

After that put your source code in the directory. If you run the python file now, probably It won’t launch If you are using non-native modules. You can install those modules runing

pip3 install <module> or pip install <module> 

This will not affect you entire module list except the environment you are In.

Now you can execute the command at the top and now you have a requirements file which contains only the modules you installed in the virtual environment. Now you can run the command at the top.

I advise everyone to use environments as It makes things easier when It comes to stuff like this.

Hope this helped.


回答 4

如果与我面临同样的问题,即不在虚拟环境上,并且想要特定项目或从所选文件夹(包括子文件夹)和pipreqs获得requirements.txt,则不支持。

您可以使用 :

import os
import sys
from fuzzywuzzy import fuzz
import subprocess

path = "C:/Users/Username/Desktop/DjangoProjects/restAPItest"


files = os.listdir(path)
pyfiles = []
for root, dirs, files in os.walk(path):
      for file in files:
        if file.endswith('.py'):
              pyfiles.append(os.path.join(root, file))

stopWords = ['from', 'import',',','.']

importables = []

for file in pyfiles:
    with open(file) as f:
        content = f.readlines()

        for line in content:
            if "import" in line:
                for sw in stopWords:
                    line = ' '.join(line.split(sw))

                importables.append(line.strip().split(' ')[0])

importables = set(importables)

subprocess.call(f"pip freeze > {path}/requirements.txt", shell=True)

with open(path+'/requirements.txt') as req:
    modules = req.readlines()
    modules = {m.split('=')[0].lower() : m for m in modules}


notList = [''.join(i.split('_')) for i in sys.builtin_module_names]+['os']

new_requirements = []
for req_module in importables:
    try :
        new_requirements.append(modules[req_module])

    except KeyError:
        for k,v in modules.items():
            if len(req_module)>1 and req_module not in notList:
                if fuzz.partial_ratio(req_module,k) > 90:
                    new_requirements.append(modules[k])

new_requirements = [i for i in set(new_requirements)]

new_requirements

with open(path+'/requirements.txt','w') as req:
    req.write(''.join(new_requirements))

PS:在检查Fuzzylogic时,可能还有一些其他库。

If Facing the same issue as mine i.e. not on the virtual environment and wants requirements.txt for a specific project or from the selected folder(includes children) and pipreqs is not supporting.

You can use :

import os
import sys
from fuzzywuzzy import fuzz
import subprocess

path = "C:/Users/Username/Desktop/DjangoProjects/restAPItest"


files = os.listdir(path)
pyfiles = []
for root, dirs, files in os.walk(path):
      for file in files:
        if file.endswith('.py'):
              pyfiles.append(os.path.join(root, file))

stopWords = ['from', 'import',',','.']

importables = []

for file in pyfiles:
    with open(file) as f:
        content = f.readlines()

        for line in content:
            if "import" in line:
                for sw in stopWords:
                    line = ' '.join(line.split(sw))

                importables.append(line.strip().split(' ')[0])

importables = set(importables)

subprocess.call(f"pip freeze > {path}/requirements.txt", shell=True)

with open(path+'/requirements.txt') as req:
    modules = req.readlines()
    modules = {m.split('=')[0].lower() : m for m in modules}


notList = [''.join(i.split('_')) for i in sys.builtin_module_names]+['os']

new_requirements = []
for req_module in importables:
    try :
        new_requirements.append(modules[req_module])

    except KeyError:
        for k,v in modules.items():
            if len(req_module)>1 and req_module not in notList:
                if fuzz.partial_ratio(req_module,k) > 90:
                    new_requirements.append(modules[k])

new_requirements = [i for i in set(new_requirements)]

new_requirements

with open(path+'/requirements.txt','w') as req:
    req.write(''.join(new_requirements))

P.S: It may have a few additional libraries as it checks on fuzzylogic.


回答 5

Python 3的最佳方法是:

pip3 freeze > requirements.txt

对我有用

best way for Python 3 is:

pip3 freeze > requirements.txt

it worked for me…


回答 6

如果您使用的是PyCharm,则在您将项目打开或克隆到PyCharm时,它会显示警报,并要求您安装所有必需的软件包。

if you are using PyCharm, when you open or clone the project into the PyCharm it shows an alert and ask you for installing all necessary packages.