Skip to content Skip to sidebar Skip to footer

Installing Modules In Python 3

Suppose I have a directory with custom .py files. The directory is called useful_scripts and a subdirectory called tested_scripts which also contains scripts (.py files). I've s

Solution 1:

If you have multiple modules (Python file with .py ) in directory and want to import one module in another module then first define that directory to a python directory or package

Packages are namespaces which contain multiple packages and modules.Each package in Python is a directory which MUST contain a special file called __init__.py

Python Package

Your directory structure should be like this if you want to import modules or package

enter image description here

Now you can import module a.py in module b.py or module b.py in module a.py

If you want to install custom lib then create setup.py the file where coustomlib directory exists ( create setup.py outside the coustomlib directory or along coustomlib )

in setup.py

#!/usr/bin/env pythonfrom distutils.core import setup
from setuptools import setup, find_packages

setup(name='coustomlib',
  version='1.0',
  description='Python coustom lib ',
  author='your name',
  author_email='name@mail.in',
  packages=find_packages(),
 )    

for install run

python setup.py install

After install coustomlib you can import it any module

import coustomlib

Or

from coustomlib.module1 import a

More info about setup.py

Post a Comment for "Installing Modules In Python 3"