Thursday, September 11, 2025

How to set up python environment for my project?

I came to python from java world. Initially, I struggled with setting up virtual environment for a new python project or for a new project I just git cloned. One of the reason being there are so many ways and it is so confusing ( pyenv, venv, poetry, virtualenv, pipenv, pip, conda, uv, pyenv-virtualenv.... ). Though lot of information is available on net, it still confusing because, I feel, most of these "How to..." posts miss very very basic information. So that prompted me to write this. 
Here I will show 2 ways. First one, I will be using pyenv. Second, I will be using uv. uv package is latest and suggest you to use it. 

Remember this. You can't set up python version and virtual environment everything in one go. So, one easy way is: First you need to take care of Python version you want to use. I use pyenv to manage Python on my laptop.

HERE IS 'A WAY'. 

When you get your MacBook, (apple stop giving default python since 12.3 ) first install a python. You can download from their official website or use Homebrew. I suggest using home brew, so it is easy to install or remove later. So once you have python install pyenv. Use brew again. Now use pyenv to manage the python version you need and set up environment. So if I do it today( 09/10/2025) I get python version 3.13.7. Let say I need 3.10.9 for my project, this is what I have to do.

( skip below 6 steps, if you have it already)

brew install python
brew install pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zprofile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zprofile
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'eval "$(pyenv init -)"' >> ~/.zprofile



pyenv versions  <== list available python versions.
pyenv  install --list  <== list installed python versions.
pyenv install 3.10.9  <== installs specified python version.
cd ~/my_python_proj
pyenv local 3.10.9  <== this will create '.pyenv-version' file and set python version 
python -m venv .my_python_proj <== create virtual env; use proj name. easy to recognize
source .my_python_proj/bin/activate <== activate virtual env
which python  <== points to your python in your virtual env.
which pip  <== points to pip in your virtual env.
pip install request <== install any packages you want in your virtual env.
deactivate <== you will come out of virtual env 
pyenv global 3.13.7  <== this will be default python unless set with 'local'.
 


Now if you open this project in visual code studio, VS studio will 
automatically recognize the local python version and recommends you same. 

How to set up using uv?

uv is latest python package manager and is very fast. it is available as downloadable as well as python package. Don't try to install it as python package or download (using shell program). Install it as homebrew package. This way it is independent of python and also easy to upgrade/remove etc. 

brew install uv
uv venv --python 3.11 .venv
source .venv/bin/activate
deactivate






Monday, April 17, 2017

Little beyond simplest RESTful web service with embedded jetty

Over this weekend (April/2017) I tried to write simple RESTful webservice (by the way I don't remember writing REST for work in last few years). So, I started where everyone starts i.e. google. With in no time, I am able to write a simple REST service with jetty server running in embedded mode.

Then I want to find more examples of how to add a HTML form and submit to a REST service i.e. use POST, not just GET. I found few examples, but all of them starting the servlet engine (jetty or grizzly or Tomcat) as a server and serving HTML or servlet content. NOT a single example that shows how to serve HTML and REST service while running jetty in embedded mode. So I decided to code one and blog it.

I used maven (3.x) and java 8.

You can download the maven project (with ALL dependencies) from here:
https://github.com/gputty/SimpleRestService

Now lets go how to do it step by step (github project may NOT match exactly)

Step #1
Create a project using maven command:
mvn archetype:generate -DgroupId=com.yaams -DartifactId=SimpleRestService -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Lets first build pom.xml with all dependencies.

Below tags tell you what versions used.
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.jetty.version>9.4.3.v20170317</project.jetty.version>
        <project.jersey.version>2.7</project.jersey.version>
    </properties>

First add all jetty dependencies:
        <!-- jetty server dependencies -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${project.jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${project.jetty.version}</version>
        </dependency>

Then add jersey dependencies:

        <!-- jersey dependencies -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${project.jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>${project.jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>${project.jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>${project.jersey.version}</version>
        </dependency>

Below are optional jars :

        <!--  below are optional dependencies -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
        </dependency>


Step # 2

Now look at main code:

Add context handlers for web folder
        // adding "web" folder
        ResourceHandler handler = new ResourceHandler();
        handler.setDirectoriesListed(true);
        handler.setWelcomeFiles(new String[] { "index.html" });
        handler.setResourceBase("./web");      
        ContextHandler context = new ContextHandler();
        context.setContextPath("/web");
        context.setHandler(handler);  
        contextsCollection.addHandler(context);

Add jersey servlet context handler :
So any REST request will be handled like any other request by servlet engine. ONLY difference is before handing over to servlet engine, we need to hand over it to jeresey servlet org.glassfish.jersey.servlet.ServletContainer and that will figure out which service we are trying to invoke and rest will be handled as usual.

        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContextHandler.setContextPath("/rest");
        ServletHolder jerseyServlet = servletContextHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
        jerseyServlet.setInitOrder(0);      
        jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", ContactService.class.getCanonicalName());

 Once you have these context handlers add them to ContextHandlerCollection.

If someone wonders what is "embedded mode". it is simply you start jetty server in your own java program rather than starting it as a server from command line. So when you want to shutdown your server, you will just kill your java program.
Now create jetty server in embedded mode and set the handlers:

Server jettyEmbeddedServer = new Server(8080);
jettyEmbeddedServer.setHandler(mainProgram.contextsCollection);

Now start it:
        try {
            jettyEmbeddedServer.start();
            jettyEmbeddedServer.join();
        } finally {
            jettyEmbeddedServer.destroy();
        }

That's it.




Friday, February 12, 2016

CREATING ORACLE PROCEDURE USING JAVA JDBC

Today tried writing a quick jdbc program to create an Oracle stored procedure dynamically. It was so bizarre, that jdbc creates procedure but status is invalid. So finally found reason. Typically stored procedures end with '/' (don't know why - may be signal to Oracle about end of program). Once removed it then it started working OK.

Here is sample JDBC & stored procedure:

CREATE OR REPLACE PROCEDURE TEST1 (CNT IN NUMBER)
AS FIRSTNUM NUMBER DEFAULT 0;
BEGIN
FIRSTNUM := 1;
DELETE FROM SCOTT.TEST; COMMIT;
FOR I IN 1 .. CNT LOOP
FIRSTNUM := FIRSTNUM + 1;
INSERT INTO SCOTT.TEST VALUES(FIRSTNUM,'XXXX');
COMMIT;
END LOOP;
END TEST1;


Sunday, September 27, 2015

Creating a web service project using java using jersey, maven and eclipse

Look at https://jersey.java.net/documentation/latest/getting-started.html

I am using mac. Make sure you have java( > 1.7) , maven.

mkdir ~/temp
cd ~/temp


mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.webservices.example -DartifactId=simple-webservice -Dpackage=com.webservices.example \
-DarchetypeVersion=2.22
cd simple-webservice
mvn clean compile
mvn eclipse:eclipse













http://localhost:8080/myapp/myresource


Tuesday, July 7, 2015

Persisting (to MySQL) java objects using EclipseLink - JPA

This article explains how to create a JPA application using EclipseLink. JPA stands for Java Persistence API a standard that defines how to persist java objects directly to a data base. The other popular java standard is JDO. EclipseLink is an implementation of JPA standard done by Oracle and open sourced. TopLink is their paid product.
I tried JPA (I might use JPA and EL interchangeably, please read based on context) with several databases, including Oracle, MySQL, Derby, Mongo MS-SQL. Not just that I tried JPA with java objects are that are not designed, i.e. java objects are created during run time and persisted them to database (yes, you are right, tables need to be created during run time and JPA gives you an option to do that too) But most of that stuff will explain in later blogs.
Current blog is a simple JPA based stand alone program that persists a java objects to MySQL database.

I am using Mac OS X Yosemite (version 10.10.3) and maven version 3.1.1 and JDK 1.7.
If you don't have maven or JDK please install them first and come back.

maven archetype are kind of templates that will allow you to create pre-configured (or filled) projects. So number 623 represents a sample maven project.
623: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.  107 - is simple JPA, EclipseLink based project. )



gputty ~/temp/blog$ mvn archetype:generate   
then keep entering appropriate values.  Except groupId and artifactId you can chose default values. 
OR you can simply give the type directly and all parameters in one line and create it like below. 


 mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.gopi.jpa -DartifactId=SimpleJPAProject -Dversion=1.0.0   
 cd SimpleJPAProject   
 mvn test  
 ls -al   
 mvn eclipse:eclipse   
 ls -al  

Now you have a basic maven project is ready. 'mvn eclipse:eclipse' command will add .classpath and .project files that help to import this project directly to Eclipse IDE. Now you can open Eclipse and File --> import and point to this directory and import it.

To be continued...