I am working on a Python project that requires PIL to show images. However, the computers that I am working on often do not allow me to install things, and have a very bare bones python setup. For this reason, most of the modules that I need I simply place in the same directory as my python files.
I tried doing the same with PIL. I downloaded the pillow source, and copied the PIL folder into my project. I was then able to run "import PIL" with no problems. However, when I then tried to run "from PIL import Image" I get the error: "The _Imaging C module is not installed". From other searches I think that installing Pillow properly would fix this problem, however I would like PIL to be more portable, and not require an instillation.
Any ideas would be great. Thanks in advance.
One solution is bundle PIL in with the script in .egg form. Then, you can import PIL directly from the .egg instead of having to install it:
How to create Python egg file
The basic process is as follows:
How to create egg:
Edit PIL's setup.py to include from setuptools import setup instead of normal setup import
Run python setup.py bdist_egg
Egg will be inside of dist/
How to import egg:
Copy .egg file to script's directory and import desired modules:
import os
import sys
DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(DIR, "./path/to/PIL.egg"))
#You can now import from PIL normally:
from PIL import Image
Related
I am building a package (and then going to upload it on pypi). I have 3 modules in this package. These packages have a few dependencies like numpy, cv2, and more.
So, I mentioned my dependencies in the setup.py file.
# -*- coding: utf-8 -*-
import setuptools
setuptools.setup(
name='StyleTransferTensorFlow',
url='https://github.com/LordHarsh/Neural_Style_Transfer',
author='Hash Banka',
author_email='harshbanka321#gmail.com',
packages=setuptools.find_packages(),
# Needed for dependencies
install_requires=['matplotlib','tensorflow','os-win','ffmpy','glob2', 'pytest-shutil', 'pytube', 'opencv-python', 'pillow', 'numpy', 'tensorflow_hub'],
version='0.0.8',
# The license can be anything you like
license='MIT',
description="Package to apply style transfer on different frames of a video",
# We will also need a readme eventually (there will be a warning)
# long_description=open('README.txt').read(),
python_requires='>=3.6',
)
I have also imported them in the init.py file present in same directory as the modules.
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
from pytube import YouTube
import os
import cv2
from PIL import Image
import shutil
import glob
import ffmpy
But still i am getting error when I execute code in the module
NameError: name 'cv2' is not defined
I am not sure why my imports are not running.
I have used cv2 inside a module to do the task.
So I am not sure what I am doing wrong. Please help me out.
I am not sure you fully understand what install_requires and packages are for.
install_requires specifies libraries that are required for your installation through setup.py to work. E.g. when installing it with pip or python setup.py install. You are specifying what should be on your computer before installing for the installation to work. No package will be installed: if a package listed here is missing, it will simply throw you an error during installation.
A very common package to include there is numpy, as you may import it in the setup.py, for example if you have some C or FORTRAN code which needs to compile upon installation.
packages argument shows which packages are needed for your library to work, so basically which packages to install along with the library. It will check if the package is not already installed on your machine, and if not install it along the library.
What I would do is empty entirely the install_requires argument. Don't even specify it if you are importing none of these packages in the setup.py. If it still doesn't work, I would replace setuptools.find_packages() with the list you are currently providing to install_requires.
I am using Pillow-6.1.0 for my python project and I am trying to import the ImageGrab() function inside it however I keep getting this following error:
cannot import name '_imaging' from 'PIL'
I am currently using Python 3.7 and editing the code on Visual Studios (Unsure if the IDE may play part in the error as I do not have much experience on it)
I am also running Windows so some of the solutions I have seen which require Linux does not work for me.
What I have tried:
Uninstalling and reinstalling Pillow
Deleting everything with "PIL" in the Python37-32/Lib/site-packages folder and uninstalling Pillow and reinstalling
Moving the Pillow module directly into my project folder
Moving ImageGrab.py directly into my project folder
Updated my Pillow module
All my reinstallations of Pillow is via the pip command
Other possibly relevant info:
ImageGrab() is not the only module that I cant import, I also cant import the Image module
I can import the PIL module successfully but I cannot access the modules within it via the command "from PIL import some_PIL_module" in my IDE
I only get the "cannot import name '_imaging' from 'PIL'" error when I am running my code through the Visual Studios IDE, if I type the command "from PIL import ImageGrab" directly into the python terminal it runs successfully with no errors
This is the line that gives me the error in my code
from PIL import ImageGrab;
I expected it to import with no problems but instead, I get the following error:
cannot import name '_imaging' from 'PIL'
I'm trying to run CairoSVG but I don't have permission on the server to run it. No problem I think, I've used other libraries in Python before without installing them by pointing sys.path.append to their directory path:
import sys
sys.path.append("/full/path/to/SomeOtherLibrary")
import SomeOtherLibrary
I thought I could create something like loader.py, import the path inside there to CairoSVG with a sys.path.append, then instead of calling: cairosvg image.svg -o image.png I could call /full/path/to/loader.py image.svg -o image.png.
Then I started to create loader.py and thought I have no idea what to do or if this is even possible. I figured I start with:
import sys
sys.path.append("/full/path/to/CairoSVG")
And then I don't know what. If I was able to install the script I'd need to pass parameters to it. How can I create a loader to run this library?
Simply call cairosvg.main() from your script. Pretty much like it is done here: https://github.com/Kozea/CairoSVG/blob/master/cairosvg.py
I have the following, very simple python code in a lambda function:
from sklearn.externals import joblib
import praw
import datetime
from operator import attrgetter
import sys
def handler_name(event, context):
return "I am a cat dog and i meow."
I have also done pip installs for scikit-learn, praw, datetime, numpy and scipy from within a python 2.7 virtualenv. I then compressed my .py file along with everything in my virtualenv's /lib/python2.7/site-packages folder into a zip and uploaded it to AWS lambda. Unfortunately when I run the code I get the following error:
Unable to import module 'mainLambda': /var/task/sklearn/__check_build/_check_build.so: invalid ELF header
___________________________________________________________________________
Contents of /var/task/sklearn/__check_build:
setup.py _check_build.so __init__.pyc
__init__.py setup.pyc
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.
If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
Obviously the issue is with sk-learn. I have no idea what though. It could be a versioning issue but I downloaded all the libraries from within a virtulenv and chose a python2.7 lambda function. Any idea? I am stumped!
from datasets import dataset_utils ImportError: No module named datasets.
when i am writing this in python sript.
import tensorflow as tf
from datasets import dataset_utils
slim = tf.contrib.slim
But i am getting error.
from datasets import dataset_utils
ImportError: No module named datasets
I found this solution
How can jupyter access a new tensorflow module installed in the right path?
I did the same and i have dataset packages at path anaconda/lib/python2.7/site-packages/. Still i am getting same error.
pip install datasets
I solved it this way.
You can find the folder address on your device and append it to system path.
import sys
sys.path.append(r"D:\Python35\models\slim\datasets"); import dataset_utils
You'll need to do the same with 'nets' and 'preprocessing'
sys.path.append(r"D:\Python35\models\slim\nets"); import vgg
sys.path.append(r"D:\Python35\models\slim\preprocessing"); import vgg_preprocessing
Datasets is present in https://github.com/tensorflow/models/tree/master/slim/datasets
Since 'models' are not installable from pip (at the time of writing), they are not available in python load paths by default. So either we copy them or manually add to the path.
Here is how I setup env before running the code:
# git clone or wget
wget https://github.com/tensorflow/models/archive/master.zip -O models.zip
unzip models.zip
# add it to Python PATH
export PYTHONPATH=$PYTHONPATH:$PWD/models-master/slim
# now we are good to call `python mytensorflow.py`
It's using the datasets package in the TF-slim image models library, which is in:
git clone https://github.com/tensorflow/models/
Having done that though, in order to import the module as shown in the example on the slim image page, empty init.py have to be added to the models and models/slim directories.
go to https://github.com/nschaetti/EchoTorch/releases and download the latest release
install the latest release from the downloaded file (202006291 is the latest version at the moment):
$pip install ./EchoTorch-202006291.zip
test it out using narma10_esn.py (other examples may have some issues)
you may still need to install some more python packages not listed in the requirements file but it works once you do this.