# Example of calling Haxe code from Python program?

**URL:** https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202
**Category:** Haxe
**Tags:** haxe-python
**Created:** [January 7, 2020, 1:27am UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202 "2020-01-07T01:27:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)
#### Post date: [January 7, 2020, 1:27am UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/1 "2020-01-07T01:27:53Z")

</div>

How I can arrange things in my project such that I can call my own Haxe library code from a Python script?

Thanks!

---

<div class="post-metadata">

### Author: ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)
#### Post date: [January 8, 2020, 2:54am UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/2 "2020-01-08T02:54:05Z")

</div>

I realize that I need to first compile my Haxe code to Python, and then access the _resulting_ Python code from Python.

Ah. Ok. I need to back up: How can set up and build my own Haxe library, compiling it to a Python library?

Does this look ok and customary for Haxe?

```haxe
$ cd ~/dev/my-haxe-libs/my-lib-foo
$ tree -F
.
├── LICENSE.txt
├── py.hxml
├── pylib/
│ └── my_pkg/
├── README.md
└── src/
    └── my_pkg/
        └── MyModule.hx

```

where my py.hxml file looks like:

```haxe
-p src
-m my_pkg.MyModule
--python pylib/my_pkg/MyModule.py

```

and MyModule.hx contains:

```haxe
package my_pkg;

class MyModule {
    public static function foo() {
        trace("hi");
    }
}

```

but running `haxe py.hxml` gives me this error:

```haxe
$ haxe py.hxml 
src/my_pkg/MyModule.hx:3: lines 3-7 : Invalid -main : my_pkg.MyModule does not have static function main

```

I don’t have a `main` because this is a library, but commenting out the “`-m`” line from py.hxml leaves me with the compiler apparently not happy with it.

Thanks!

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [January 8, 2020, 8:01am UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/3 "2020-01-08T08:01:39Z")

</div>

Just omit the `-m`, but keep the rest of the line - you’re not compiling a main class, you want to compile that module.

See also: [Compiling libraries without main class - Compilation - Haxe programming language cookbook](https://code.haxe.org/category/compilation/compiling-libraries-without-main-class.html)

---

<div class="post-metadata">

### Author: ![aliokan](https://community.haxe.org/user_avatar/community.haxe.org/aliokan/32/182_2.png) [@aliokan](https://community.haxe.org/u/aliokan)
#### Post date: [January 8, 2020, 8:04am UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/4 "2020-01-08T08:04:36Z")

</div>

Hello,  
If you want to export your code as a library, you do not have to define main class.  
You can find an example of library export here: [Compiling libraries without main class - Compilation - Haxe programming language cookbook](https://code.haxe.org/category/compilation/compiling-libraries-without-main-class.html)

---

<div class="post-metadata">

### Author: ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)
#### Post date: [January 8, 2020, 8:46pm UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/5 "2020-01-08T20:46:18Z")

</div>

Ooof!! Got it now. Thanks!

---

<div class="post-metadata">

### Author: ![uvtc](https://community.haxe.org/user_avatar/community.haxe.org/uvtc/32/1198_2.png) [@uvtc](https://community.haxe.org/u/uvtc)
#### Post date: [January 8, 2020, 9:07pm UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/6 "2020-01-08T21:07:13Z")

</div>

Funny. Although my haxe code is in package `my_pkg`, the resulting Python module is not, but instead the class is named “`my_pkg_MyModule`”.

Anyhow, it works. Thanks!

---

<div class="post-metadata">

### Author: ![Gama11](https://community.haxe.org/user_avatar/community.haxe.org/gama11/32/7_2.png) [@Gama11](https://community.haxe.org/u/Gama11)
#### Post date: [January 8, 2020, 9:20pm UTC](https://community.haxe.org/t/example-of-calling-haxe-code-from-python-program/2202/7 "2020-01-08T21:20:39Z")

</div>

On some targets like Python and JS, packages are “flattened”, which helps performance a bit. On JS. you can avoid this with `-D js-unflatten`, doesn’t look like there’s an equivalent for Python.
