我的自定义Django应用程序代码在哪里?

时间:2016-02-25 08:53:19

标签: python django pip

我在官方教程https://docs.djangoproject.com/en/1.8/intro/reusable-apps/

之后构建并安装了我的自定义Django应用程序

安装似乎很成功。

$ pip install --user ../horizon2fa-0.1.tar.gz

Processing /opt/stack/horizon2fa-0.1.tar.gz
  Requirement already satisfied (use --upgrade to upgrade): horizon2fa==0.1 from file:///opt/stack/horizon2fa-0.1.tar.gz in /opt/stack/.local/lib/python2.7/site-packages
Building wheels for collected packages: horizon2fa
  Running setup.py bdist_wheel for horizon2fa ... done
  Stored in directory: /opt/stack/.cache/pip/wheels/a6/4a/f0/4533f85d90b8f1a274a35d3865a2e0b15ff85f0570a0708679
Successfully built horizon2fa

在哪里可以找到所有自定义类和方法的源代码?

我试图通过我的系统搜索它,但没有找到它们。代码是编译好的吗?

$ sudo find / -name "*horizon2fa*"

/root/.cache/pip/wheels/a0/9d/24/d8070ea2a01759ce7ebc03c34393db8a5aceccd380e60481c5/horizon2fa-0.1-cp27-none-any.whl
/opt/stack/.cache/pip/wheels/a6/4a/f0/4533f85d90b8f1a274a35d3865a2e0b15ff85f0570a0708679/horizon2fa-0.1-cp27-none-any.whl
/opt/stack/.local/lib/python2.7/site-packages/horizon2fa-0.1.dist-info
/opt/stack/horizon2fa-0.1.tar.gz

模块似乎安装不正确。

python -c "import horizon2fa; print(horizon2fa.__path__)"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named horizon2fa

下面,您可以看到我的应用目录结构。

trex@trex:~/Development/openstack2FA/horizon2fa$ tree
.
├── admin.py
├── dist
│   └── horizon2fa-0.1.tar.gz
├── enabled
│   └── _31000_myplugin.py
├── horizon2fa.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── __init__.py
├── LICENSE
├── main.py
├── MANIFEST.in
├── migrations
│   ├── 0001_initial.py
│   └── __init__.py
├── models.py
├── panel.py
├── README.rst
├── setup.py
├── templates
│   ├── base.html
│   └── horizon2fa
│       ├── created.html
│       ├── index.html
│       ├── login.html
│       ├── new.html
│       └── view.html
├── tests.py
├── urls.py
├── user.py
└── views.py

我的setup.py脚本。

import os
from setuptools import find_packages, setup

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
    README = readme.read()

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name='horizon2fa',
    version='0.1',
    packages=find_packages(),
    include_package_data=True,
    license='BSD License',  # example license
    description='A Django app.',
    long_description=README,
    url='http://www.trex.com/',
    author='trex',
    author_email='trex@trex.com',
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Framework :: Django :: X.Y',  # replace "X.Y" as appropriate
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',  # example license
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        # Replace these appropriately if you are stuck on Python 2.
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)

1 个答案:

答案 0 :(得分:1)

因为您使用了--user,所以会为当前用户(发出pip install --user)而不是系统site-packages目录中安装该程序包。请参阅site.USER_BASE的文档。 因此,您应该按照文档中的说明查看~/.local/,可能是:/home/%user%/.local/lib/python%version%/site-packages/

另外,考虑到包已经安装在PYTHONPATH的某个地方,您可以尝试通过在shell中运行以下命令来找到它:

python -c "import %module%; print(%module%.__path__)"

python -c "import horizon2fa; print(horizon2fa.__path__)"

关于您的问题更新:

您应该创建一个顶级目录,例如django-horizon2fa并将setup.pyMANIFEST.inREADME.RSTLICENSE.txt和您的horizon2fa软件包目录放在其中。因此,安装相关文件位于新目录中,并且与模块相关的所有文件都位于该目录中的目录内。当前目录设置不允许find_packages()正常工作。

django-horizon2fa
│
├── LICENSE
├── MANIFEST.in
├── README.rst
├── setup.py
└── horizon2fa
    ├── __init__.py
    ├── admin.py
    ├── tests.py
    ├── urls.py
    ├── user.py
    ├── views.py
    ├── enabled
    │   └── _31000_myplugin.py
    ├── main.py
    ├── migrations
    │   ├── 0001_initial.py
    │   └── __init__.py
    ├── models.py
    ├── panel.py
    ├── tests.py
    ├── urls.py
    ├── user.py
    ├──  views.py
    └── templates
        ├── base.html
        └── horizon2fa
            ├── created.html
            ├── index.html
            ├── login.html
            ├── new.html
            └── view.html

P.S。仅使用MANIFEST.in,有时可能会导致在分发中包含数据包数据的问题,例如模板。在这种情况下,请考虑将MANIFEST.in字典中的package_data文件提供给setup(),请参阅details in the docs