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脚本就全复制到新目录了,再把其它相关文件复制过去,干净清新的新工程就诞生了。

Keval allows you to call arbitrary Windows kernel-mode functions from user mode, even (and primarily) on another machine.

Keval Keval allows you to call arbitrary Windows kernel-mode functions from user mode, even (and primarily) on another machine. The user mode portion

42 Dec 17, 2022
Python tool to check a web applications compliance with OWASP HTTP response headers best practices

Check Your Head A quick and easy way to check a web applications response headers!

Zak 6 Nov 09, 2021
Deep Difference and search of any Python object/data.

DeepDiff v 5.6.0 DeepDiff Overview DeepDiff: Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all t

Sep Dehpour 1.6k Jan 08, 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
Control-Alt-Delete - Help Tux Escape Beastie's Jail!

Control-Alt-Delete Help Tux escape Beastie's jail by completing the following challenges! Challenges Challenge 00: Drinks: Tux needs to drink less. Ch

NDLUG 8 Oct 31, 2021
Early version for manipulate Geo localization data trough API REST.

Backend para obtener los datos (beta) Descripción El servidor está diseñado para recibir y almacenar datos enviados en forma de JSON por una aplicació

Víctor Omar Vento Hernández 1 Nov 14, 2021
Simple yet flexible natural sorting in Python.

natsort Simple yet flexible natural sorting in Python. Source Code: https://github.com/SethMMorton/natsort Downloads: https://pypi.org/project/natsort

Seth Morton 712 Dec 23, 2022
Format Norminette Output!

Format Norminette Output!

7 Apr 19, 2022
The producer-consumer problem implemented with threads in Python

This was developed using a Python virtual environment, I would strongly recommend to do the same if you want to clone this repository. How to run this

Omar Beltran 1 Oct 30, 2021
Minimal Windows system information tool written in Python

wfetch wfetch is a Minimal Windows system information tool written in Python (Only works on Windows) Installation First of all have python installed.

zJairO 3 Jan 24, 2022
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
Python library to decorate and beautify strings

outputformat Python library to decorate and beautify your standard output 💖 Ins

Felipe Delestro Matos 259 Dec 13, 2022
Casefy (/keɪsfaɪ/) is a lightweight Python package to convert the casing of strings

Casefy (/keɪsfaɪ/) is a lightweight Python package to convert the casing of strings. It has no third-party dependencies and supports Unicode.

Diego Miguel Lozano 12 Jan 08, 2023
An extremely simple package with a single utillity class used for gracefully handling POSIX shutdown signals.

graceful-killer An extremely simple package with a single utillity class used for gracefully handling POSIX shutdown signals. Installation Use pip to

Sven Ćurković 1 Dec 09, 2021
✨ Un générateur de mot de passe aléatoire totalement fait en Python par moi, et en français.

Password Generator ❗ Un générateur de mot de passe aléatoire totalement fait en Python par moi, et en français. 🔮 Grâce a une au module random et str

MrGabin 3 Jul 29, 2021
A python script to generate wallpaper

wallpaper eits Warning You need to set the path to Robot Mono font in the source code. (Settings are in the main function) Usage A script that given a

Henrique Tsuyoshi Yara 5 Dec 02, 2021
A script to parse and display buy_tag and sell_reason for freqtrade backtesting trades

freqtrade-buyreasons A script to parse and display buy_tag and sell_reason for freqtrade backtesting trades Usage Copy the buy_reasons.py script into

Robert Davey 31 Jan 01, 2023
Factoral Methods using two different method

Factoral-Methods-using-two-different-method Here, I am finding the factorial of a number by using two different method. The first method is by using f

Sachin Vinayak Dabhade 4 Sep 24, 2021
Python program to do with percentages and chances, random generation.

Chances and Percentages Python program to do with percentages and chances, random generation. What is this? This small program will generate a list wi

n0 3 Jul 15, 2021
UUID version 7, which are time-sortable (following the Peabody RFC4122 draft)

uuid7 - time-sortable UUIDs This module implements the version 7 UUIDs, proposed by Peabody and Davis in https://www.ietf.org/id/draft-peabody-dispatc

Steve Simmons 22 Dec 20, 2022