Adventures in Python

Attempting to embrace Python

I try to like python. I really do. I attempt to use it and be like all the other cool people. Sadly, I am a dumb python noob. I see an import like:

from Crypto.Cipher import AES

Cool, lets install the crypto pacakge…

pip install crypto

It returns command not found: pip, fair enough. I am not a python dev, my environment isn’t setup. Lets run the module.

python3 -m pip install crypto

More errors:

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with

    brew install pipx

    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.

    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.

    Read more about this behavior here: <https://peps.python.org/pep-0668/>

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

Ok, I guess venv’s are no longer a suggestion and instead a way of life.

mkdir -p  ~/python-sucks/venv-for-crypto
python3 -m venv  ~/python-sucks/venv-for-crypto
source ~/python-sucks/venv-for-crypto/bin/activate

hey it works!

Collecting crypto
  Using cached crypto-1.4.1-py2.py3-none-any.whl.metadata (3.4 kB)
Collecting Naked (from crypto)
  Using cached Naked-0.1.32-py2.py3-none-any.whl.metadata (931 bytes)
Collecting shellescape (from crypto)
  Using cached shellescape-3.8.1-py2.py3-none-any.whl.metadata (2.8 kB)
Collecting requests (from Naked->crypto)
  Using cached requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Collecting pyyaml (from Naked->crypto)
  Using cached PyYAML-6.0.2-cp313-cp313-macosx_11_0_arm64.whl.metadata (2.1 kB)
Collecting charset-normalizer<4,>=2 (from requests->Naked->crypto)
  Using cached charset_normalizer-3.4.1-cp313-cp313-macosx_10_13_universal2.whl.metadata (35 kB)
Collecting idna<4,>=2.5 (from requests->Naked->crypto)
  Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting urllib3<3,>=1.21.1 (from requests->Naked->crypto)
  Using cached urllib3-2.3.0-py3-none-any.whl.metadata (6.5 kB)
Collecting certifi>=2017.4.17 (from requests->Naked->crypto)
  Using cached certifi-2024.12.14-py3-none-any.whl.metadata (2.3 kB)
Using cached crypto-1.4.1-py2.py3-none-any.whl (18 kB)
Using cached Naked-0.1.32-py2.py3-none-any.whl (587 kB)
Using cached shellescape-3.8.1-py2.py3-none-any.whl (3.1 kB)
Using cached PyYAML-6.0.2-cp313-cp313-macosx_11_0_arm64.whl (171 kB)
Using cached requests-2.32.3-py3-none-any.whl (64 kB)
Using cached certifi-2024.12.14-py3-none-any.whl (164 kB)
Using cached charset_normalizer-3.4.1-cp313-cp313-macosx_10_13_universal2.whl (195 kB)
Using cached idna-3.10-py3-none-any.whl (70 kB)
Using cached urllib3-2.3.0-py3-none-any.whl (128 kB)
Installing collected packages: shellescape, urllib3, pyyaml, idna, charset-normalizer, certifi, requests, Naked, crypto
Successfully installed Naked-0.1.32 certifi-2024.12.14 charset-normalizer-3.4.1 crypto-1.4.1 idna-3.10 pyyaml-6.0.2 requests-2.32.3 shellescape-3.8.1 urllib3-2.3.0

Lets have some fun and start writing python.

from Crypto.Cipher import AES

Sadness…

Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    from Crypto.Cipher import AES
ModuleNotFoundError: No module named 'Crypto'

cant find the Crypto module. oh hey. I am on a mac, I guess i need some special hacks. I seen this in some random code.

import sys
import crypto
sys.modules["Crypto"] = crypto
from Crypto.Cipher import AES

More Sadness…

Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    from Crypto.Cipher import AES
ModuleNotFoundError: No module named 'Crypto.Cipher'

oh, this is for the Crypto.Cipher. Lets look at our package.

ls ~/python-sucks/venv-for-crypto/lib/python3.13/site-packages/crypto

hrmm. no cipher folder.

__init__.py    __pycache__    app.py         decryptoapp.py library        settings.py

oh hey. I think I need pycrypto instead of crypto.

python3 -m pip install pycrypto

Lets see if it works

import sys
import crypto
sys.modules["Crypto"] = crypto
from Crypto.Cipher import AES

yay! it does. it feels wrong. what did this just do? it installed pycrypto package in the existing crypto folder? yes it did. hooray for case insensitive file systems combined with removing the py prefix on the installed module.

Lets create a new venv and install the right module.

mkdir -p  ~/python-sucks/venv-for-crypto-2
python3 -m venv  ~/python-sucks/venv-for-crypto-2
source ~/python-sucks/venv-for-crypto-2/bin/activate
python3 -m pip install pycrypto

and finally try just the initial line that started this adventure.

from Crypto.Cipher import AES

Yes! It works the way it should.

Last modified January 9, 2025: Update 2025-01-09-python.md (1c8d5ab)