4

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...

abhay gurrala
  • 171
  • 1
  • 6
  • 18

2 Answers2

2

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
Community
  • 1
  • 1
Miro Rodozov
  • 197
  • 1
  • 10
  • I am installing the package with setup.py.So,my command to install is `python setup.py install` and not with pip.I think your answer is suitable for packages which are to be installed with pip.Any suggestions how to do it manually? – abhay gurrala Aug 12 '16 at 09:39
  • Now i read the question more carefully and it seems you just have a environmental variable problem, because /usr/local/bin is not in your $PYTHONPATH. Try to do this - add pemanently /usr/local/bin to your $PYTHONPATH like this: 1. Open your ~/.bashrc file and add this to it export PYTHONPATH="${PYTHONPATH}:/my/other/path" logout, login to load the ~/.bashrc and try to import the package. I've done it myself when i had packages installed outside the default site-package folder. Let me know how it goes – Miro Rodozov Aug 12 '16 at 09:48
  • Yeah,that's what i got to know when i was searching for the answers.So,there is no fix directly?I mean with other ways because I want to distribute this code. – abhay gurrala Aug 12 '16 at 09:52
  • Well apart from adding additional path to the $PYTHONPATH , i don't see other way how the python executable would know there are packages in /usr/local/bin , so there may be other ways but - why bother :) this seems straightforward – Miro Rodozov Aug 12 '16 at 09:54
  • As I said,I want to distribute the code to people,I don't want to see them facing problems like me.So,that's why I need a simpler way :) – abhay gurrala Aug 12 '16 at 09:57
  • Hm, i think setuptools default install location is in /usr/local/bin. So there is an option to prefix the install path, and you'll give that prefix path the $PYTHONPATH default in your setup script - e voila ! What do you think ? Here how you describe the prefix : there is package_dir = {'foo': 'lib'} option , you may try to get the $PYTHONPATH var and pass it as argument to that option – Miro Rodozov Aug 12 '16 at 10:04
  • https://docs.python.org/2/distutils/setupscript.html#listing-whole-packages Here it is how you describe the install location. So if you use this in the script you can install it at the default python packages folder - given you have inspected the PYTHONPATH var first. This should work – Miro Rodozov Aug 12 '16 at 10:08
1

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
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • Perfect.But still i didn't get the second part i.e., create a wrapper (shell or python) capable of calling your installed script and install that via scripts=[...]. What should i keep in scripts and what is wrapper?Sorry for inconvenience... – abhay gurrala Aug 12 '16 at 10:06
  • do you have a `def main()` and a `if __name__ == "__main__": main()`? – Laur Ivan Aug 12 '16 at 10:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120789/discussion-between-laur-ivan-and-abhay-gurrala). – Laur Ivan Aug 12 '16 at 10:22