Find dependent python scripts of a python script in a project directory.

Overview

检测一个python脚本依赖的python脚本,听起来有点绕,先说一下背景吧。

在做python开发的时候,随着日积月累,工程目录下会有很多py脚本,而在部署的时候只需要用到其中一小部分,无关的脚本很多,感觉很不清爽,人为一个一个去筛吧,又很麻烦,有没有工具可以自动把相关的py脚本抽出来呢?如果有,请告诉我!我没有找到,但是仔细想一下好像可以自己实现。

在部署工程的时候,通常有个主程序(py脚本),但光有主程序还不够,主程序一般会依赖其它模块,有些是自己写的py脚本,工程稍微大一点就会出现层层依赖,人为去查又很麻烦,文章标题的意思就是,怎么自动揪出主程序依赖的脚本呢?

要揪出来的是那些自己开发的脚本(或者是搬运过来的),反正不是那些安装的模块。因为安装的模块不需要找,部署的时候自然会安装。

假设有一个工程的目录结构如下

.
├── demo_main.py
├── demo_package	
    └── demo_module.py

demo_main.py是主程序,demo_package是一个文件夹,里面包含demo_module.py。主程序文件里有一句from demo_package import demo_module,它依赖demo_module.py

python demo_module.py执行主程序之后,工程目录下生成一个子目录__pycache__,里面有一个.pyc文件,从名字可以看出来是与demo_module.py对应的。也就是,执行一个py脚本以后,它所依赖的py脚本会有一个对应的.pyc文件生成。

.
├── demo_main.py
└── demo_package
    ├── __pycache__
    │   └── demo_module.cpython-37.pyc
    └── demo_module.py

而且,.pyc文件的位置和命名是有规律的。__pycache__demo_module.py在同一个目录下,demo_module.cpython-37.pycdemo_module.py前缀相同。

那么,思路就有了:

  1. 执行一下主程序
  2. 找到工程目录下生成的.pyc文件
  3. 根据.pyc文件的位置和名称推出py脚本的位置和名称
  4. 把py脚本copy到新的工程目录
  5. 把主程序也复制过去

这样,需要的py脚本就全复制到新目录了,再把其它相关文件复制过去,干净清新的新工程就诞生了。

More routines for operating on iterables, beyond itertools

More Itertools Python's itertools library is a gem - you can compose elegant solutions for a variety of problems with the functions it provides. In mo

2.9k Jan 06, 2023
Find version automatically based on git tags and commit messages.

GIT-CONVENTIONAL-VERSION Find version automatically based on git tags and commit messages. The tool is very specific in its function, so it is very fl

0 Nov 07, 2021
Python type-checker written in Rust

pravda Python type-checker written in Rust Features Fully typed with annotations and checked with mypy, PEP561 compatible Add yours! Installation pip

wemake.services 31 Oct 21, 2022
A simple gpsd client and python library.

gpsdclient A small and simple gpsd client and library Installation Needs Python 3 (no other dependencies). If you want to use the library, use pip: pi

Thomas Feldmann 33 Nov 24, 2022
This code renames subtitle file names to your video files names, so you don't need to rename them manually.

Rename Subtitle This code renames your subtitle file names to your video file names so you don't need to do it manually Note: It only works for series

Mostafa Kazemi 4 Sep 12, 2021
Python Classes Without Boilerplate

attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka d

The attrs Cabal 4.6k Jan 06, 2023
A Program that generates and checks Stripe keys 24x7.

A Program that generates and checks Stripe keys 24x7. This was made only for Educational Purposes, I'm not responsible for the damages cause by you

iNaveen 18 Dec 17, 2022
🍰 ConnectMP - An easy and efficient way to share data between Processes in Python.

ConnectMP - Taking Multi-Process Data Sharing to the moon 🚀 Contribute · Community · Documentation 🎫 Introduction : 🍤 ConnectMP is the easiest and

Aiden Ellis 1 Dec 24, 2021
ULID implementation for Python

What is this? This is a port of the original JavaScript ULID implementation to Python. A ULID is a universally unique lexicographically sortable ident

Martin Domke 158 Jan 04, 2023
A python lib for generate random string and digits and special characters or A combination of them

A python lib for generate random string and digits and special characters or A combination of them

Torham 4 Nov 15, 2022
The Black shade analyser and comparison tool.

diff-shades The Black shade analyser and comparison tool. AKA Richard's personal take at a better black-primer (by stealing ideas from mypy-primer) :p

Richard Si 10 Apr 29, 2022
DUQ is a python package for working with physical Dimensions, Units, and Quantities.

DUQ is a python package for working with physical Dimensions, Units, and Quantities.

2 Nov 02, 2022
RapidFuzz is a fast string matching library for Python and C++

RapidFuzz is a fast string matching library for Python and C++, which is using the string similarity calculations from FuzzyWuzzy

Max Bachmann 1.7k Jan 04, 2023
A small python tool to get relevant values from SRI invoices

SriInvoiceProcessing A small python tool to get relevant values from SRI invoices Some useful info to run the tool Login into your SRI account and ret

Wladymir Brborich 2 Jan 07, 2022
A string extractor module for python

A string extractor module for python

Fayas Noushad 4 Jul 19, 2022
Hot reloading for Python

Hot reloading for Python

Olivier Breuleux 769 Jan 03, 2023
EVE-NG tools, A Utility to make operations with EVE-NG more friendly.

EVE-NG tools, A Utility to make operations with EVE-NG more friendly. Also it support different snapshot operations with same style as Libvirt/KVM

Bassem Aly 8 Jan 05, 2023
A utility that makes it easy to work with Python projects containing lots of packages, of which you only want to develop some.

Mixed development source packages on top of stable constraints using pip mxdev [mɪks dɛv] is a utility that makes it easy to work with Python projects

BlueDynamics Alliance 6 Jun 08, 2022
Script to autocompound 3commas BO:SO based on user provided risk factor

3commas_compounder Script to autocompound 3commas BO:SO based on user provided risk factor Setup Step 1 git clone this repo into your working director

0 Feb 24, 2022
A tool to create the basics of a project

Project-Scheduler Instalação Para instalar o Project Maker, você necessita está em um ambiente de desenvolvimento Linux ou wsl com alguma distro debia

2 Dec 17, 2021