Skip to content

Commit

Permalink
Update content src/site/notes/ProgrammingLanguages/python/modules/查看p…
Browse files Browse the repository at this point in the history
…y module路径.md
  • Loading branch information
1024daniel committed Sep 19, 2024
1 parent 228e0ec commit 2f56f52
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,30 @@ from numpy.fft import fft
print(sys.modules['numpy'])
print(sys.modules['numpy.fft'])

```


判断一个module是否已经安装(包含requirement)
```py
import importlib
import pkg_resources
from pkg_resources import get_distribution

def is_installed(package: str)-> bool:
importlib.reload(pkg_resources)
try:
get_distribution(package)
return True
except pkg_resources.DistributionNotFound:
return False
except:
print('get distribution error')
return False

package='numpy==1.26.4'
if is_installed(package):
print(package, 'is installed')
else:
print(package, 'is not installed')

```

0 comments on commit 2f56f52

Please sign in to comment.