python 黑客工具开发_构建没有路径黑客的python项目

I have a shared python library that I use in multiple projects, so the structure looks like this:

Project1

main.py

...

sharedlib

__init__.py

ps_lib.py

another.py

Now in each project's main.py I use the following hack to make it work:

import os

import sys

sys.path.insert(0, os.path.abspath('..'))

import sharedlib.ps_lib

...

Is there a way to do it without using this hack? Or is there a better way to organize the projects structure?

解决方案

I think the best way would be to make sharedlib a real package. That means changing the structure a bit:

sharedlib/

sharedlib/

__init__.py

ps_lib.py

another.py

setup.py

And using something like this in the setup.py (taken partially from Python-packaging "Minimal Structure"):

from setuptools import setup

setup(name='sharedlib',

version='0.1',

description='...',

license='...',

packages=['sharedlib'], # you might need to change this if you have subfolders.

zip_safe=False)

Then install it with python setup.py develop or pip install -e . when in the root folder of the sharedlib package.

That way (using the develop or -e option) changes to the contents of sharedlib/sharedlib/* files will be visible without re-installing the sharedlib package - although you may need to restart the interpreter if you're working in an interactive interpreter. That's because the interpreter caches already imported packages.

From the setuptools documentation:

Setuptools allows you to deploy your projects for use in a common directory or staging area, but without copying any files. Thus, you can edit each project’s code in its checkout directory, and only need to run build commands when you change a project’s C extensions or similarly compiled files. [...]

To do this, use the setup.py develop command.

(emphasis mine)

The most important thing is that you can import sharedlib everywhere now - no need to insert the sharedlib package in the PATH or PYTHONPATH anymore because Python (or at least the Python where you installed it) now treats sharedlib like any other installed package.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值