I am trying to make a python package which I want to install using pip install .
locally. The package name is listed in pip freeze
but import <package>
results in an error No module named <package>
. Also the site-packages folder does only contain a dist-info folder. find_packages()
is able to find packages. What am I missing?
import io
import os
import sys
from shutil import rmtree
from setuptools import find_packages, setup, Command
# Package meta-data.
NAME = '<package>'
DESCRIPTION = 'description'
URL = ''
EMAIL = 'email'
AUTHOR = 'name'
# What packages are required for this module to be executed?
REQUIRED = [
# 'requests', 'maya', 'records',
]
# The rest you shouldn't have to touch too much :)
# ------------------------------------------------
# Except, perhaps the License and Trove Classifiers!
# If you do change the License, remember to change the Trove Classifier for that!
here = os.path.abspath(os.path.dirname(__file__))
# Where the magic happens:
setup(
name=NAME,
#version=about['__version__'],
description=DESCRIPTION,
# long_description=long_description,
author=AUTHOR,
author_email=EMAIL,
url=URL,
packages=find_packages(),
# If your package is a single module, use this instead of 'packages':
# py_modules=['mypackage'],
# entry_points={
# 'console_scripts': ['mycli=mymodule:cli'],
# },
install_requires=REQUIRED,
include_package_data=True,
license='MIT',
classifiers=[
# Trove classifiers
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy'
],
)
Since the question has become quite popular, here are the diagnosis steps to go through when you're missing files after installation. Imagine having an example project with the following structure:
Now I run
pip install .
, check that the package is installed:but see neither
spam
, norspam/eggs.py
norbacon.py
norspam/fizz/buzz.py
in the list of files belonging to the installed package:So what to do now?
Diagnose by inspecting the wheel build log
Unless told not to do so,
pip
will always try to build a wheel file and install your package from it. We can inspect the log for the wheel build process if reinstalling in the verbose mode. First step is to uninstall the package:then install it again, but now with an additional argument:
Now if I inspect the log:
I notice that no files from the
spam
directory orbacon.py
are mentioned anywhere. This means they were simply not included in the wheel file and hence not installed bypip
. The most common error sources are:Missing packages: check the
packages
argumentVerify you have passed the
packages
argument to the setup function. Check that you have mentioned all of the packages that should be installed. Subpackages will not be collected automatically if only the parent package is mentioned! For example, in the setup scriptspam
will be installed, but notspam.fizz
because it is a package itself and must be mentioned explicitly. Fixing it:If you have lots of packages, use
setuptools.find_packages
to automate the process:In case you are missing a module:
Missing modules: check the
py_modules
argumentIn the above examples, I will be missing
bacon.py
after installation since it doesn't belong to any package. I have to provide its module name in the separate argumentpy_modules
:Missing data files: check the
package_data
argumentI have all the source code files in place now, but the
data.txt
file is still not installed. Data files located under package directories should be added via thepackage_data
argument. Fixing the above setup script:Don't be tempted to use the
data_files
argument. Place the data files under a package and configurepackage_data
instead.After fixing the setup script, verify the package files are in place after installation
If I now reinstall the package, I will notice all of the files are added to the wheel:
They will also be visible in the list of files belonging to
mypkg
:If you are on Windows 10+, one way you could make sure that you had all the correct installations was to click start in the bottom left-hand corner and search cmd.exe and right-click on "Command Prompt" (Make sure you choose "Run as Administrator"). Type "cd
path to your Python 3.X installation
". You can find this path in File Explorer (go to the folder where Python is installed) and then at the top. Copy this, and put it in where I wrote abovepath to your Python 3.X installation
. Once you do that and click enter, type "python -m pip installpackage
" (package
signifies the package you would like to install). Your Python program should now work perfectly.