1

I am trying to create a python package so after creating the folder with all my files in it including a __init__.py file I moved the folder into /usr/local/lib/python3.8/site-packages/.

When I try to use import ClockTower(name of the folder) It returns with

Exception has occurred: ModuleNotFoundError
No module named 'ClockTower'

what am I doing wrong?

Seaver Olson
  • 450
  • 3
  • 16

1 Answers1

1

The reason that you're not able to import ClockTower successfully is that you haven't actually installed it.

distutils

If the package is just for personal use on your own machine I would recommend using distutils to create a simple setup.py script to enable installation of your package - distutils is often distributed as a built-in package, however Debian based systems may not have it by default (see here).

Here is some step-by-step documentation on creating a setup.py script that will enable you to install and use your package across the system.

If you just want a quick and minimal solution, first create a new directory, then create a file called setup.py within this folder put the directory containing package files/code inside also.

Your enclosing folder should look something like this - additional files/directories in ClockTower are omitted here for clarity.

/home/usr/Desktop/EnclosingDir
├───setup.py
│
└───ClockTower
    └───__init__.py

setup.py should look something like this to define a basic install of the ClockTower package.

from distutils.core import setup

setup(
    name='ClockTower',
    version='1.0.0',
    packages=['ClockTower'],
)

To install your package for use by the specified interpreter (here python3.8) , simply navigate to the enclosing folder that contains setup.py and run.

python3.8 -m pip install .

You should see some console output and if the install has been successful you should see ClockTower 1.0.0 listed when you run python3.8 -m pip list and be able to successfully import ClockTower using this interpreter.

Important note: This approach does not account for any external dependencies (e.g. non-built-in packages/modules) of ClockTower, it assumes that these will be available by the interpreter - e.g. already installed using something like a requirements.txt install procedure.

setuptools

setuptools is a much more fully featured library for distributing Python packages. This feature-richness comes at the cost of increased complexity/time to learn and implement.

If you are going to be sharing this package with others I would recommend checking out the setuptools user guide and following along to create a robust and portable distribution of your package.

JPI93
  • 1,507
  • 5
  • 10