简体   繁体   中英

How do I install a python package to /usr/local/bin?

I am trying to install a python package on my ubuntu.I am trying to install it through a setup script which i had written.The setup.py script looks like this:

    from setuptools import setup

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

setup(
    name = 'pyduino',
    description = 'PyDuino project aims to make python interactive with hardware particularly arduino.',
    url = '###',
    keywords = 'python arduino',
    author = '###',
    author_email = '###',
    version = '0.0.0',
    license = 'GNU',
    packages = ['pyduino'],
    install_requires = ['pyserial'],
    classifiers = [

        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools', 
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],
    scripts=['pyduino/pyduino.py'],
)

Package installs in /usr/local/bin directory.But when I am importing the modules outside the /usr/local/bin,import error occurs.I tried changing path to /usr/local/bin and it works perfectly and import error doesn't occur.How can I install the package so that I can import the modules in any directory? Thanks in advance...

Try install your packages with pip using this

pip install --install-option="--prefix=$PREFIX_PATH" package_name

as described here Install a Python package into a different directory using pip? and i'll suggest to read what are 1. pip 2. virtualenv

Good luck :)

EDIT: i found the package is installed with pip like:

pip install --install-option="--prefix=/usr/local/bin" pyduino_mk

Currently, you're using a scripts tag to install your python code. This will put your code in /usr/local/bin , which is not in PYTHONPATH .

According to the documentation , you use scripts when you want to install executable scripts (stuff you want to call from command line). Otherwise, you need to use packages .

My approach would be like this:

  • install the pyduino/pyduino.py in the library with something like packages=['pyduino']
  • create a wrapper (shell or python) capable of calling your installed script and install that via scripts=[...]

Using the packages tag for your module will install it in /usr/local/lib/python... , which is in PYTHONPATH . This will allow you to import your script with something like import pyduino.pyduino.* .

For the wrapper script part:

A best practice is to isolate the code to be executed if the script is triggered from command line in something like:

def main():
    # insert your code here
    pass

if __name__ == '__main__':
    main()
  • Assuming there is a def main() as above
  • create a directory scripts in your tree (at the same level with setup.py )
  • create a file scripts/pyduino
  • in scripts/pyduino :

     #!/usr/bin/env python from pydiuno.pyduino import main if __name__ == '__main__': main()
  • add a `scripts = ['scripts/pyduino'] to your setup.py code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM