Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

python - Importing package in another package's module?

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.

question from:https://stackoverflow.com/questions/65940759/importing-package-in-another-packages-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...