<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anthony Wainer Cachay Guivin on Medium]]></title>
        <description><![CDATA[Stories by Anthony Wainer Cachay Guivin on Medium]]></description>
        <link>https://medium.com/@awainerc?source=rss-6b833b6d8ff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*y6aNCzgQKj6XfBNjn6r3pw.jpeg</url>
            <title>Stories by Anthony Wainer Cachay Guivin on Medium</title>
            <link>https://medium.com/@awainerc?source=rss-6b833b6d8ff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:24:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@awainerc/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integrating Pyspark with Pycharm + Pytest]]></title>
            <link>https://awainerc.medium.com/integrating-pyspark-with-pycharm-pytest-f1234356c9a6?source=rss-6b833b6d8ff------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1234356c9a6</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pyspark]]></category>
            <category><![CDATA[pytest]]></category>
            <category><![CDATA[pycharm]]></category>
            <dc:creator><![CDATA[Anthony Wainer Cachay Guivin]]></dc:creator>
            <pubDate>Tue, 13 Apr 2021 01:17:40 GMT</pubDate>
            <atom:updated>2021-04-13T02:26:27.094Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*Djy1AD23B245zT3TkREiTQ.png" /></figure><p>Using Pyspark with current versions when working locally, often ends up being a headache. Especially when we are against time and need to test as soon as possible.</p><p><strong>1- Install prerequisites<br>2- Install PyCharm<br>3- Create a Project<br>4- Install PySpark with PyCharm<br>5- Testing Pyspark with Pytest</strong></p><p>Here are some tips or advantages of working Pyspark with Pycharm in local.</p><h4>1- Install prerequisites</h4><p>The most important tip to avoid dying in the race is to ensure support for the <a href="https://www.py4j.org/install.html">Py4J</a> driver, which is the one that will allow us to interact using Python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*UhLuF6ap-prmytFx8grwBA.png" /><figcaption><a href="https://www.py4j.org/install.html">https://www.py4j.org/install.html</a></figcaption></figure><p>As we can see at the time of writing, support for Python is greater than 2.7 and less than 3.7, and for Java greater than version 7, importantly something that is not in the picture is that it has to be less than version 11.</p><p>For this example we will use:</p><ul><li><strong>Python </strong><a href="https://www.python.org/downloads/release/python-379/"><strong>3.7.9</strong></a><strong> <br></strong><a href="https://realpython.com/installing-python/">https://realpython.com/installing-python/</a></li><li><strong>Java 8 <br></strong><a href="https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html">https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html</a></li></ul><h4>2- Install PyCharm</h4><p>I know that many don’t like to use Pycharm because it is a very heavy IDE, in my case I try to get the most out of the tool.<br>There are two ways to install this tool, one is to use the free community version and the other is to use the paid professional or academic version..</p><ul><li>Download PyCharm:<br> <a href="https://www.jetbrains.com/pycharm/download">https://www.jetbrains.com/pycharm/download</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*ejFIxJ0jjhFCaqwBw46-EA.png" /></figure><p><em>if you are a student or teacher you can apply for an educational licence. </em><a href="https://www.jetbrains.com/community/education/"><em>https://www.jetbrains.com/community/education/</em></a></p><p><strong>Installation guide:</strong></p><p><a href="https://www.jetbrains.com/help/pycharm/installation-guide.html">Install PyCharm | PyCharm</a></p><h4>3- Create a Project</h4><p>Open Pycharm and go to New Project</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*y9AbUbgBsLk35-rfohYBdQ.png" /></figure><p>In this part you have several options to create a Python project, for best practices we create a project using the VirtualEnv option that will allow us to have separate Python libraries.</p><p>In <strong>Location</strong> you can define the project name and in which part of your machine you want your project to be saved, and in the <strong>Base Interpreter</strong> you can choose the Python version installed, in this case the project name is <strong>pysparkProject </strong>and the python version is 3.7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*Qu7shdWc-eiYXeLtFMou3g.png" /></figure><p>Click on create and wait a little while our project is configured.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yYWvgSBtVFfCZRfIxviC6Q.png" /><figcaption>well done!</figcaption></figure><h4>4- Install PySpark with PyCharm</h4><p>The second tip I give you, try to get the most out of Pycharm, if you are a console lover, you can use PIP.<br>This part is intended to streamline the tasks.</p><p>In Pycharm, go to the settings:<br><strong>file &gt; Settings</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*-HtPdz6fAs__wDFLAesA4Q.png" /></figure><p>In settings, go to Python Interpreter<br><strong>Project &gt; Python Interpreter</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-6p13lXMtRc9v0xBwAwwcA.png" /></figure><p>This is the most important part because it depends on the Spark version for our project works. <br>Click on the + button, search for PySpark and install.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*An5dn6NSL7JdZUfCi7RcrQ.png" /></figure><p>We take advantage of the packages window to install <a href="https://pypi.org/project/pyspark-stubs/"><strong>pyspark-stubs</strong></a>, which will allow us to work with Spark’s native functions. This fixes the common error: <a href="https://stackoverflow.com/questions/40163106/cannot-find-col-function-in-pyspark"><strong>cannot find the lit, col, etc. function.</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S72xmTo7ZCu6Zx-6b8XT7w.png" /></figure><p>We check our installation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t7QKVs9PWL7QJFqAQ5GHng.png" /></figure><p><strong>Download Apache Spark: </strong><br><a href="https://spark.apache.org/downloads.html">https://spark.apache.org/downloads.html</a><br>Remember to download the version of Spark that is compatible with Pyspark, for example for Pyspark we downloaded version 3.1.1, so we will download that version in spark. Regarding the Hadoop version, it is up to you to choose the version you need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*PmTa97FTzTh-3sxdzqRiwg.png" /></figure><p>Unzip and place it in the root path or a path that has permissions in your machine.</p><h4><strong>5- </strong>Testing <strong>Pyspark with Pytest</strong></h4><p>Install Pytest, to test our Pyspark code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*6TIujGb0rdqTXwsM-pQ5-w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*e-vUr-l-vxex_IHQlUV_7Q.png" /></figure><p>Create <strong>test_main.py<br>New&gt;Python File</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*NTP-Z0UH9R6nQITVojTKWw.png" /></figure><p>Select Python file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*vvBW7Z1paD6O0BNF2p-G2w.png" /></figure><p>In the code of <strong>test_main.py, </strong>Import Pytest</p><pre><em>import </em>pytest</pre><p>Import SparkSession and create a function named spark for our spark session.</p><pre><em>from </em>pyspark.sql.session <em>import </em>SparkSession</pre><pre>@pytest.fixture<br><em>def </em>spark():<br>    <em>return </em>SparkSession.builder.appName(&quot;test&quot;).getOrCreate()</pre><p>Test that our version of Pyspak is correct, for that we create a function called <strong>test_spark_version </strong>that has <strong>spark </strong>as attribute:</p><pre><em>def </em>test_spark_version(spark):<br>    <em>assert </em>spark.version == &quot;3.1.1&quot;</pre><p>Test to create data from a range, create a function called <strong>test_create_data_from_range.</strong></p><pre><em>def </em>test_create_data_from_range(spark):<br>    data = spark.range(1, 7, 2).collect()<br><br>    result = [Row(id=1), Row(id=3), Row(id=5)]<br><br>    <em>assert </em>data == result</pre><p>Test to create a dataframe from data, create a function called <strong>test_create_dataframe.</strong></p><pre><em>def </em>test_create_dataframe(spark):<br>    data = [Row(name=&quot;abc&quot;, id=1)]<br>    df = spark.createDataFrame(data)<br><br>    <em>assert </em>df <em>is not None</em></pre><p>Test to count dataframe, create a function called <strong>test_count_dataframe.</strong></p><pre><em>def </em>test_count_dataframe(spark):<br>    data = [Row(name=&quot;abc&quot;, id=1)]<br>    df = spark.createDataFrame(data)<br><br>    <em>assert </em>df.count() == 1</pre><blockquote>All possible cases are validated in order to determine the errors that can be generated by.</blockquote><p>to run the test is set up like this, go to the top under <strong>ADD CONFIGURATION.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*CfB7JciuTCRFtX-8oup1cw.png" /></figure><p>Click on the <strong>+</strong> button and scroll down to <strong>Python Test</strong>, then select <strong>pytest</strong>.<br><strong>+ &gt; Python Test &gt; pytest</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*m40SL0iHD30xSTPK7vN5BA.png" /></figure><p>We add a name to our test, in the Script path part we select test_main.py, in the Enviroment Variables, we put our spark home and in case of error the python path, separated by <strong>;</strong><br><strong>SPARK_HOME=spark-3.1.1-<em>bin</em>-hadoop3.2;PYSPARK_PYTHON=python</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cuBvHVkAVr_cR0Mu8_e-mQ.png" /></figure><p>click on the play button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*eOOjn8E3ueqWVVL8phd8sw.png" /></figure><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R3eDnuMibYhtRWiQ_3sINQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc4222cb4d37b63c781bb678939cacf1/href">https://medium.com/media/dc4222cb4d37b63c781bb678939cacf1/href</a></iframe><blockquote>As we can see, the importance of first doing our tests before coding is very important, because we can determine all the possible errors and other benefits, I recommend you to investigate about TDD, it will change your life philosophy.</blockquote><p>The last step is joining and coding in <strong>Main.py</strong></p><pre><em>from </em>pyspark.sql.session <em>import </em>SparkSession<br><em>from </em>pyspark.sql.types <em>import </em>Row<br><br><em>if </em>__name__ == &#39;__main__&#39;:<br>    spark = SparkSession.builder.appName(&quot;test[*]&quot;).getOrCreate()<br>    <em>print</em>(&quot;Spark Init:&quot;, spark.version)<br><br>    data = [Row(name=&quot;abc&quot;, id=1)]<br>    df = spark.createDataFrame(data)<br>    df.show()<br><br>    <em>print</em>(f&quot;count: {df.count()}&quot;)</pre><p>click on the play button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*DkhycHS-9Zyi7O7ClGQN9Q.png" /></figure><p>At the moment of executing we will get an error because our SPARK_HOME is not configured, go to edit configuration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*9ZaMWohv2MIhHjqjt0E8iQ.png" /></figure><p>Add the environment variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*zKmtLXB4Elg8nJcTJ-Uwkg.png" /></figure><p>Run and wait for the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uHLOiCAWFMHTd5aAP1KDYQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2b3100c60e71fca663ac28bd6c5b9a3/href">https://medium.com/media/c2b3100c60e71fca663ac28bd6c5b9a3/href</a></iframe><p><strong>Thank you very much for reading, I would appreciate it if you could share it and comment if you have any input or doubts. Good vibes</strong></p><p>Link gitHub repo:<br><a href="https://github.com/AnthonyWainer/pyspark_pytest">https://github.com/AnthonyWainer/pyspark_pytest</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1234356c9a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>