Reputation: 1162
I'd like to make a wheel binary distribution, intstall it and then import it in python. My steps are
python ./my_package/setup.py bdist_wheel
pip install ./dist/*.whl
python -c"import my_package"
This leads to the error:
ImportError: No module named 'my_package'
Also, when I do pip list
, the my_package
is listed.
However, when I run which my_packge
, nothing is shown.
When I run pip install ./my_package/
everything works as expected.
How would I correctly build and install a wheel?
python version 3.5 pip version 10.1 wheel version 0.31.1
UPDATE:
When I look at the files inside my_package-1.0.0.dist-info, there is an unexpected entry in top_level.txt
. It is the name of the folder where I ran
python ./my_package/setup.py bdist_wheel
in. I believe my setup.py
is broken.
UPDATE WITH REGARDS TO ACCEPTED ANSWER:
I accepted the answer below. Yet, I think it is better to simply cd
into the package directory. Changing to a different directory as suggested below leads to unexpected behavior when using the -d
flag, i.e. the target directory where to save the wheel. This would be relative to the directory specified in the setup.py file.
Upvotes: 5
Views: 6207
Reputation: 440
In case anyone is using pyproject.toml
and setuptools
in conjunction with setup.py
and setup.cfg
, there is a note in the documentation that eluded me for quite a while. Standard directory structures for packages will be auto-detected, but if you are using a non-standard directory structure you may need to find packages, or list them explicitly. I was using what I thought was a standard directory structure, but it was not:
/path/to/myproject/
├── mypackage/ Python package directory.
│ ├── __init__.py This makes the directory a package.
│ └── example_module.py An example module.
├── pyproject.toml Definition of build process of the package.
├── README.md README with info of the project.
└── setup.cfg Configuration details of the python package.
What I had to do in pyproject.toml
was either this:
[tool.setuptools]
packages = ["mypackage"]
or this:
[tool.setuptools.packages.find]
where = ["."]
Another point of confusion that may arise is that the name of the wheel installed by pip (ie derived from 'myproject') and which pip list
subsequently shows as simply 'myproject', is not the importable package (ie 'mypackage').
Upvotes: 0
Reputation: 11
I have experienced the same situation, maybe not for the same reason, here just for reference. The package name should not contain the dash "-", there's no error pop out, but after installing your wheel, though it is shown in pip list, you can't find that package.
/src/your-package-name # should not
/src/your_package_name # should like this
In the setup.py, you can use the name with dash "-" without limitation:
setuptools.setup(
name="instrument-lab",
...
Upvotes: 0
Reputation: 1258
In my case, in order to solve it I just had to upgrade pip (since Docker installed pip 9).
python3 -m pip install --upgrade pip
Upvotes: 0
Reputation: 41
I had the very same error, but it was due to my setup.py not specifying the entry "packages=setuptools.find_packages()". Everythings builds nicely without that but you can't import anything even though pip shows it to be installed.
Upvotes: 4
Reputation: 66161
If you need to execute the setup script from another directory, ensure you are entering the project dir in the script.
from setuptools import setup
root = os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))
os.chdir(root)
# or using pathlib (Python>=3.4):
import patlib
root = pathlib.Path(__file__).parent
os.chdir(str(root))
setup(...)
Upvotes: 1