This article will explain how to install virtualenv and create a new Python Project in eclipse which uses a custom virtual environment. There is also a little Hello World style example at the end!
This article assumes that you have Python, Eclipse and the PyDev plugin installed on your machine. If you need to know how to do this, please see my previous post: Setting up your Windows 10 System for Python Development (PyDev, Eclipse, Python).
In the current public version of Python, there is a bug in one of the packages which will display the following error message if you try to install any packages using pip:
Fatal error in launcher: Job information querying failed
This bug has been fixed in the current dev release but still exists in the production version at the time of writing this article. If you want to learn more about the status of this, you can track it via this ticket: http://bugs.python.org/issue24127.
OK let’s get started.
Let me just re-iterate, this article assumes that you have Python installed with the necessary path variables set. If you have not done this or are in doubt, please follow this first: Setting up your Windows 10 System for Python Development (PyDev, Eclipse, Python).
Add Python scripts directory to system path
First, we add the python scripts directory to the system path. We do this so we can feel like we are using a unix based system where you can just type commands line pip into the console and they work (without having to type the full executable URL). Right click the Windows menu and select System.
From the System menu, select Advanced system settings.
From the System Properties menu, select the Advanced tab and click on Environment Variables…
From the System variables section, location the Path variable, select it and click Edit…
From the Edit System Variable menu, navigate to the end of the Variable value and append:
(note the semi colon which is used to separate the items in this list. Once done, click OK.
Click OK on the Environment Variables screen.
On the System Properties screen, click OK.
Now exit the System menu by clicking X.
Install Virtualenv Using Pip and Create Your First Environment
Right click the Windows button and select Command Prompt (Admin). We load it in Admin mode because we are installing packages which will likely require elevated rights.
In the Administrator: Command Prompt type the following command to install virtualenv.
python -m pip install virtualenv
The reason we use the “-m” switch is because as mentioned at the beginning of this post, there is a bug in Python for Windows 10 which causes an error to occur when trying to install with pip.
After it’s installed, type exit to quit Command Prompt.
Next, load command prompt again but this time under your user account, by right clicking the Windows buttons and choosing Command Prompt.
Command prompt should start in “C:\Users\<username>\”. Create a new directory called virtualenvs by typing in the following command:
Then switch to that new directory by typing:
Now we will create a new virtual environment called london_app_dev. A “virtual environment” is a place where all the required Python packages and settings can be stored for a specific project. This allows you to have a unique set of installed packages for each project as opposed to having one shared set. This is useful if some projects require different versions of the same package for example. Create the virtual environment by typing the following command:
python -m virtualenv london_app_dev
Feel free to specify your own name by replacing london_app_dev with whatever you want. Just remember to what you named it, for the rest of the tutorial.
Create Your First Python Project in Eclipse
OK, now we have the virtual environment created, run Eclipse.
Eclipse will load.
Once Eclipse loads, select File > New > PyDev Project from the menu.
Enter a Project name. I use the word Kaizen for this demonstration. Click the Please configure an interpreter before proceeding link to configure an interpreter (the thing that understands Python code)
In the Configure interpreter menu, choose Manual Config.
In the Preferences screen, select New… next to the Python Interpreters section.
In the Select interpreter screen, enter the Interpreter Name for your project (in this case, LondonAppDeveloper). In some cases you would give it a generic name such as Python, however because we are giving the project it’s own virtual environment we will give it a name specific to the project. After typing a name, click Browse.
In the Open screen, navigate to C:\Users\<user>\virtualenvs\london_app_dev\Scripts (where <user> is your username), and choose python from the list. Then click Open.
In the Select interpreter screen, select OK to save the settings and close it.
You will then be prompted which files you would like to add to the pythonpath. Leave all selected and choose OK.
Marvel at the Python Interpreter you have just added and select OK.
If a firewall request screen pops up for Eclipse, select Allow access.
Eclipse will process your selection of Python interpreter.Wait patiently.
Now, back to the PyDev Project window, from the Interpreter drop down list, select LondonAppDeveloper.
Now you have chosen an Interpreter, click on Finish.
Now we will create our first Python file, on the project you just created (Kaizen), right click and select New > File.
In the New File screen, type the name hello_kaizen.py and click Finish.
On the Default Eclipse preferences for PyDev screen, leave the default selection and click OK.
Your new file should automatically open. Enter the following code:
import datetime def hello_kaizen(): print "Hello Kaizen: %s" % (datetime.datetime.now()) hello_kaizen()
This will print the words Hello Kaizen and the current date and time when ran.
After you have written your script, select Run As… from the top menu.
From the Run As menu, choose Python Run and click OK.
The console screen will appear at the bottom Eclipse, if you see something like the below response, you are done!
Thank you for reading, I hope this walk-through added some value to your life 🙂