twine: ModuleNotFoundError: No module named 'twine'

Run all the step in this tutorial https://packaging.python.org/tutorials/packaging-projects/, but when run the step

twine upload --repository-url https://test.pypi.org/legacy/ dist/*

It throw this error

twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Traceback (most recent call last):
  File "/Users/kiennguyen/Library/Python/3.6/bin/twine", line 7, in <module>
    from twine.__main__ import main
ModuleNotFoundError: No module named 'twine'

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

I also had this issue.

The problem is following the instruction

python3 -m pip install --user --upgrade twine

without reading the command closely.

This (rightly) installs the twine package into the venv, but (wrongly) puts a copy of the executable in ./local/bin, which (rightly) isn’t on the path while the venv is activated.

There is a warning message about this in the pip output:

  Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Installing collected packages: pkginfo, chardet, idna, certifi, urllib3, requests, requests-toolbelt, six, docutils, Pygments, webencodings, bleach, readme-renderer, tqdm, twine
  The script pkginfo is installed in '~/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script chardetect is installed in '~/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script pygmentize is installed in '~/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script tqdm is installed in '~/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script twine is installed in '~/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed Pygments-2.3.1 bleach-3.1.0 certifi-2018.11.29 chardet-3.0.4 docutils-0.14 idna-2.8 pkginfo-1.5.0.1 readme-renderer-24.0 requests-2.21.0 requests-toolbelt-0.9.1 six-1.12.0 tqdm-4.31.1 twine-1.13.0 urllib3-1.24.1 webencodings-0.5.1

(Where ~ is replaced with the literal path to the home directory.) But it is just a warning amongst a large amount of pip output and can easily be missed or overlooked.

For completeness, I will mention I tried to fix this by copying over the executable, without avail.

cp ~/.local/bin/twine path/to/venv/bin/

The solution was to remove the --user flag so pip put the executable in the correct place.

python3 -m pip install --upgrade twine

Note that you don’t get this problem with virtualenv. If you try to install into a venv with the user flag:

virtualenv ~/venvs/tmp_venv3 -p /usr/bin/python3
source ~/venvs/tmp_venv3/bin/activate
python3 -m pip install --user --upgrade twine

You get an error message, and nothing is installed.

Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

This makes it much easier to realise the error has occurred and remove the unwanted --user flag and try again.

These are both on Python 3.5 with the latest release of pip (19.0.2).

I would say the problem lies either with venv, which should copy the behaviour of virtualenv and refuse to let pip operate inside a venv with a --user flag, or possibly with pip.

However, if you want to make the PyPI documentation more complete, you could update it to say: Install twine globally with

python3 -m pip install --user --upgrade twine

if you are installing it into a venv, omit the --user flag.

Or something along those lines.

I discovered this issue via @daudn’s comment. It sounds like @scottclowe identified an issue with the tutorial, rather than Twine. Maybe that should be reported/fixed at https://github.com/pypa/packaging.python.org/issues (if it hasn’t already).

For future reference: pipx is a handy tool for installing Python command-line tools in a dedicated virtual environment, with the executable on your $PATH. For example, without an active virtual environment:

$ pipx install twine
  installed package twine 1.13.0, Python 3.7.3
  These binaries are now globally available
    - twine
done! ✨ 🌟 ✨

$ ls -l $(which twine)
/Users/brian/.local/bin/twine -> /Users/brian/.local/pipx/venvs/twine/bin/twine