check python package location

How to Check Python Package Path

Python offers many modules and packages to do various things with your Python applications & websites. But sometimes you may need to find out the location of a Python package. There are several ways to do this in Python. In this article, we will learn how to check python package path.


How to Check Python Package Path

Every python package uses a compiled python file (.pyc) during runtime. Let us say you have a module ‘abc’ in python. You can use the following command to get the full path to its .pyc file.

import abc
print(abc.__file__)

For example, here is the command to get file path of ‘os’ module.

import django
print(django.__file__)

In the above command, you need to import the package first and then get its path.

If you don’t want to import a package before checking its location, then you can try the following command. In this case, we use os module to determine the path of a package. You can use it to determine the path of any package (e.g. abc) without actually importing.

import os
path = os.path.abspath(abc.__file__)

This method is useful if you want to check the path of many python modules.

Also, if you need to get the folder containing package, then use the following commands. Here is the command to get folder path of package ‘abc’.

import os
path = os.path.dirname(abc.__file__)

In this article, we have learnt different ways to check python package path.

Also read:

How to Take Screenshot in Ubuntu Terminal
How to Make Cross Database Queries in PostgreSQL
How to Make Cross Database Queries in MySQL
How to Copy/Transfer Data from One Database to Another in MySQL
How to Count Repeated Characters in Python String

Leave a Reply

Your email address will not be published.