Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripts in python package

I develop python application which i decided to turn into package to be installed by easy_install or pip later. I've used search to find several good sources about directory structure for python packages See this answer or this post.

I created following structure (i've omitted several files in the list to make strcture be more clear)

Project/
|-- bin/
|-- my_package/
|   |-- test/
|   |   |-- __init__.py
|   |   |-- test_server.py
|   |-- __init__.py
|   |-- server.py
|   |-- util.py
|-- doc/
|   |-- index.rst
|-- README.txt
|-- LICENSE.txt
|-- setup.py           

After that I created executable script server-run

#!/usr/bin/env python
from my_package import server

server.main()

which I placed into bin directory. If I install my package with python setup.py install or via pip/easy_install everything works fine, i can run server-run and my server starts to handle incoming requests.

But my question is how to test that server-run works in development environment (without prior installation of my_package)? Also I want to use this script to run latest server code for dev purposes.

Development happens in Project directory so i am getting ImportError if i run ./bin/server-run

user@host:~/dev/Project/$ ./bin/server-run
Traceback (most recent call last):
  File "./bin/server-run", line 2, in 
    import my_package
ImportError: No module named my_package

Is it possible to modify bin/server-run script so it will work if i run it from another folder somewhere in the filesystem (not necessarily from Project dir)? Also note that I want to use (if it is possible to achieve) the same script to run server in production environment.

like image 905
Alik Avatar asked Jul 23 '11 07:07

Alik


People also ask

What are scripts in Python?

A script is a Python file that's intended to be run directly. When you run it, it should do something. This means that scripts will often contain code written outside the scope of any classes or functions. A module is a Python file that's intended to be imported into scripts or other modules.

What is scripts in setup py?

The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.

Are Python files called scripts?

All . py files are Python modules, even scripts. Scripts are modules that act as the entry point to our Python process.


4 Answers

There is the console_scripts approach now. See e.g.

entry_points={
      'console_scripts': [
        'wikibackup = wikibot.wikipush:mainBackup',   
        'wikiedit = wikibot.wikipush:mainEdit',
        'wikinuke = wikibot.wikipush:mainNuke',
        'wikipush = wikibot.wikipush:mainPush',
        'wikiupload = wikibot.wikipush:mainUpload',
        'wikiuser = wikibot.wikiuser:main',
      ],
    },

from https://pypi.org/project/py-3rdparty-mediawiki/ (where i am a committer).

If you do a pip install of that package the above scripts will be installed as part of the installation process.

see https://github.com/WolfgangFahl/py-3rdparty-mediawiki/blob/master/setup.py for the full source code of the setup script.

like image 59
Wolfgang Fahl Avatar answered Oct 11 '22 05:10

Wolfgang Fahl


The simplest way is to configure the right Python path, so Python knows to look for my_package in the current directory.

On Linux (using Bash):

export PYTHONPATH=.
bin/server-run

On Windows:

set PYTHONPATH=.
python bin/server-run
like image 20
Søren Løvborg Avatar answered Oct 11 '22 06:10

Søren Løvborg


You need relative imports. Try

from .. import mypackage

or

from ..mypackage import server

The documentation is here

http://docs.python.org/tutorial/modules.html#intra-package-references

These work on Python 2.5 or newer.

To do it only in the development version, try:

try:
    from my_package import server
except ImportError:
    from ..my_package import server
like image 20
agf Avatar answered Oct 11 '22 06:10

agf


You can use virtualenv for testing Python code while in development as if it was released

like image 31
Mihai Maruseac Avatar answered Oct 11 '22 04:10

Mihai Maruseac