开发者

python module from source code - naming

I am a python module-making novice. Following the spirit of this question, I installed Rx to validate some data from YAML files. Wanting to keep the whole party together, I simply put the whole git tree under site-packages. This provided me a (very simplified) tree to the python implementation of site-packages/Rx/python/Rx.py.

Since this c开发者_Python百科annot be directly imported, I added __init__.py to site-packages/Rx, with a single line:

from Rx.python.Rx import *

Since it still cannot be directly imported, I added __init__.py to site-packages/Rx/python, with a single line:

from Rx import *

Now, I can successfully import. This is great, but I don't care for the following

>>> import Rx
>>> a = Rx.Factory()
>>> a
<Rx.python.Rx.Factory object at 0x00C0CCF0>

I would just like the objects to be referenced by the package name Rx, e.g., <Rx.Factory object at 0x00C0CCF0>.

How is this correctly done? Thanks!

EDIT, for anyone who might come across the same problem:

I emptied out site-packages/Rx/python/__init__.py, and changed site-packages/Rx/__init__.py, as follows:

import os
__path__.append( os.path.join(__path__[0], 'python') )
from Rx import *

This isn't exactly what I wanted, but at least now the objects are Rx.Rx.Factory, etc.


A common pattern is to setup your project like this:

ProjectName/
|-- bin/
|   `-- myscript
|-- package_name/
|   |-- __init__.py
|   |-- rx.py
`-- setup.py

Your setup script then looks like:

#!/usr/bin/env python

from distutils.core import setup

setup(name='ProjectName',
      version='1.0',
      description='My project description',
      author='Me',
      author_email='Me@localhost',
      url='http://localhost/',
      packages=['package_name'],
     )

When you run:

$ python setup.py install 

Distutils will copy your package directory into site-packages.

With the above structure your imports would look like:

from package_name import Rx
print Rx.Factory

What it sounds like you want is:

import package_name
print package_name.Factory

To do that you'll want to add the following to your init.py:

from rx import Factory

For development you could symbolically link your package directory into site-packages but there are quite a few other ways to add development code to your PYTHONPATH without symbolic links.

The simplest way to do it is to simply change directory to your project directory (cd ProjectName based on the above example) and then just execute your script from there.

If you don't like that you could append your development package to your PYTHONPATH by doing something like:

$ export PYTHONPATH=$PYTHONPATH:/home/me/ProjectName

Yet another way to do it is to use a *.pth file to add your development package to the site-packages and have it still live in your home directory. To do this you'll just create a simple file called something like myproject.pth which will include:

# pth file for Rx development
/home/me/ProjectName

The nice thing about pth files is that you can move your code around, rename the package, add a second package, etc and it will still be importable without monkeying with your symlinks.

Of course there are other options like manipulation of sys.path in your user startup files, tools like virtualenv, buildout, etc.


If there is no code in the top level Rx/python directories you canjust remove them and put the Rx.py file into site-packages.

If you want to keep the whole git tree together you can symlink the Rx file from somewhere into site-packages, but it would be better to modify your PYTHONPATH variable to include the directory that conains Rx.py.

The Python document on Modules is very comprehensive. Definitely check out the section on the module search path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜