<commit_before>---
layout: post
title: Embedding Python in C++
comments: true
---

Embedding Python in C++

This is how I embedded Python in my C++ projects. My goal was to have a console
that would take in Python and eventually plugins in Python that use an embedded
Python module.

I will also show you how to make a module and add it to your embedded Python
console.

# Setup

Get [Visual Studio 15 Express for Desktop](https://www.visualstudio.com/post-download-vs/?sku=xdesk&clcid=0x409&telem=ga#).

"C:\Users\Justin\Documents\Visual Studio 2015\Projects\EmbeddingPythonTutorial"

Get [7-Zip](http://www.7-zip.org/download.html) for compressed file needs.
Get Python 3.6. [Windows x86-64 executable installer](https://www.python.org/ftp/python/3.6.0/python-3.6.0-amd64.exe). Install Python 3.6 to "C:\Python36".

You also need the [GZipped source
tarball](https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz). This
provides all the header files we need to reference in our project. Extract the
Gzipped source tarball to your 'C' directory. You should now have "C:/Python-3.6.0".

# Coding

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen.png" width="400">
</p>

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_2.png" width="400" >
</p>

Make sure you have "x64" selected in your "Solution Platforms."

In the EmbeddedPythonTutorial Windows project. Go to the solution explorer pain
and right click the project. Then go to settings in the context menu. Then
in the window that opens go to VC++ directories. Then select "Include Directories".
Add the directory "C:\Python36\include". Add C:\Python36\libs to the "Library Directories."

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_3.png" width="400">
</p>

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_4.png" width="400">
</p>

Copy "C:\Python36\python36.dll" to "C:\Users\Justin\Documents\Visual Studio 2015\Projects\EmbeddingPythonTutorial\EmbeddingPythonTutorial" if this directory
does not exist you just need to run the program in debug mode once and it will
be created.

Now at the top of your file "EmbeddedPythonTutorial.cpp" add the following
code:

{% highlight cpp %}
// This code makes us always use the release version of Python
// even when in debug mode of this program.
// https://pytools.codeplex.com/discussions/547562
#define HAVE_ROUND<commit_msg>Looking at how GitHub pages shows images.<commit_after>---
layout: post
title: Embedding Python in C++
comments: true
---

Embedding Python in C++

This is how I embedded Python in my C++ projects. My goal was to have a console
that would take in Python and eventually plugins in Python that use an embedded
Python module.

I will also show you how to make a module and add it to your embedded Python
console.

# Setup

Get [Visual Studio 15 Express for Desktop](https://www.visualstudio.com/post-download-vs/?sku=xdesk&clcid=0x409&telem=ga#).

"C:\Users\Justin\Documents\Visual Studio 2015\Projects\EmbeddingPythonTutorial"

Get [7-Zip](http://www.7-zip.org/download.html) for compressed file needs.
Get Python 3.6. [Windows x86-64 executable installer](https://www.python.org/ftp/python/3.6.0/python-3.6.0-amd64.exe). Install Python 3.6 to "C:\Python36".

You also need the [GZipped source
tarball](https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz). This
provides all the header files we need to reference in our project. Extract the
Gzipped source tarball to your 'C' directory. You should now have "C:/Python-3.6.0".

# Coding

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen.png" width="400">
</p>

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_2.png" width="400" >
</p>

Make sure you have "x64" selected in your "Solution Platforms."

In the EmbeddedPythonTutorial Windows project. Go to the solution explorer pain
and right click the project. Then go to settings in the context menu. Then
in the window that opens go to VC++ directories. Then select "Include Directories".
Add the directory "C:\Python36\include". Add C:\Python36\libs to the "Library Directories."

![Setting up directories](https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_3.png)

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_3.png" width="400">
</p>

<p align="center">
  <img height="300" src="https://s3-us-west-1.amazonaws.com/fallrisk.de/justinwatson.name/EmbeddingPythonTutorial-Project_Screen_4.png" width="400">
</p>

Copy "C:\Python36\python36.dll" to "C:\Users\Justin\Documents\Visual Studio 2015\Projects\EmbeddingPythonTutorial\EmbeddingPythonTutorial" if this directory
does not exist you just need to run the program in debug mode once and it will
be created.

Now at the top of your file "EmbeddedPythonTutorial.cpp" add the following
code:

{% highlight cpp %}
// This code makes us always use the release version of Python
// even when in debug mode of this program.
// https://pytools.codeplex.com/discussions/547562
#define HAVE_ROUND
{'fraction_non_alphanumeric': 0.12135749822316987, 'fraction_numerical': 0.03393745557924662, 'mean_word_length': 4.629, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 17, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2357031', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1800, 'n_words': 566}