Bidirectional conversion of electrical and optical signals lies at the foundation of the global internet. Such converters are employed at repeater stations to extend the reach of long-haul fiber optic communication systems and within data centers to exchange high-speed optical signals between computers. Likewise, coherent microwave-to-optical conversion of single photons would enable the exchange of quantum states between remotely connected superconducting quantum processors, a promising quantum computing hardware platform. Despite the prospects of quantum networking, maintaining the fragile quantum state in such a conversion process with superconducting qubits has remained elusive. Here we demonstrate the conversion of a microwave-frequency excitation of a superconducting transmon qubit into an optical photon. We achieve this using an intermediary nanomechanical resonator which converts the electrical excitation of the qubit into a single phonon by means of a piezoelectric interaction, and subsequently converts the phonon to an optical photon via radiation pressure. We demonstrate optical photon generation from the qubit with a signal-to-noise greater than unity by recording quantum Rabi oscillations of the qubit through single-photon detection of the emitted light over an optical fiber. With proposed improvements in the device and external measurement set-up, such quantum transducers may lead to practical devices capable of realizing new hybrid quantum networks, and ultimately, distributed quantum computers.