Python with Ubuntu on Windows

Now that bash is on Windows, I wanted to try and make all the other guides I’d writen for Python on Windows irrelevant.

So here’s how to setup an effective environment for Python on Ubuntu on Windows.

1. Install Bash on Windows
2. Check for updates
3. Check out the REPL
4. Install Pip
5. Install VirtualEnv
6. Install VirtualEnvWrapper
7. Create your first virtualenv
8. Configure bashrc to keep it working
9. Install some packages
10. Test Flask

1. Install Bash on Windows:
Here’s the announcement blog for context.
How to Geek has a good breakdown of making it happen.

Make sure you remember your password.

Now that its installed try opening a command prompt and typing bash.
The prompt should change like this:
bashtomnt

Notice that path?
/mnt/c/Users/TimReilly

That’s your user directory for windows where your OneDrive, Documents, Desktop, etc. exist.

You can go in there now and run python scripts that might already exist, but your probably won’t have all the necessary packages installed.

Before we move forward we want to make sure Ubuntu is up to date.

2. Check for updates
From another command prompt:
lxrun /update

And inside bash
sudo apt-get update

Thanks reddit for the tips!

3. Check out the REPL

Now run python!
$ python

Should look like this:
pythonrepl

4. Install Pip

Now we’ll install Pip:
sudo apt-get install python-pip

If you have permission issues try starting an elevated prompt:
$ sudo -i
$ apt-get install python-pip
$ exit

Use exit to return to the regular prompt.
Should look like this:
sudoi
Yay we’ve got pip!
Try pip list to see what comes standard.

5. Install VirtualEnv
Now we’re basically following along with the guide presented at hitchhikersguidetopython.com

Again, you might need to start an elevated prompt to install virtualenv.

$ sudo -i
$ pip install virtualenv
$ exit
$ cd my_project_folder
$ virtualenv venv

Then to use the VirtualEnvironment

$ source venv/bin/activate

You should now see a little (venv) before your prompt.
Like this:
virtualenvfolder

Now you’ve created a virtualenv inside of your my_project_folder directory. Which is cool, but can be confusing with git, sharing code, and testing package versions.
So we use VirtualEnvWrapper to keep our virtualenvs in the same place.

Before we move on make sure you deactivate your env
deactivate

6. Install VirtualEnvWrapper

http://virtualenvwrapper.readthedocs.io/en/latest/index.html


$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

$ export WORKON_HOME=~/
Can be customized to whichever directory you’d like to place your virtualenvs

7. Create virtualenv using virtualenvwrapper

$ mkvirtualenv venv
$ workon venv
$ deactivate

Here’s an example of what it looks like to remove our venv directory and instead use venvv which will be stored in the directory underlined in red.

venvv

8. Configure bashrc to keep it working

This might not happen to you, but when I opened a new bash terminal I had to re-source my virtualenvwrapper.sh and WORKON_HOME.

So instead I added those lines to my bashrc script.

$ sudo nano ~/.bashrc
-- type in password --

This is what it looks like in nano for me.
Ctrl+X to exit and y-enter to save.
bashrc

Then either:

Source ~/.bashrc

Or start a new command prompt->bash and try “workon” or “lsvirtualenv”

See the next image for a simple workflow.

9. Install some packages

Now lets install ‘requests’ into our newly created virtualenv:
pipinstallrequests

Isn’t that nice!

10. Test Flask
Finally we’re going to test this with flask.
First we install the required files using pip into our activated ‘venv’
Then runserver -> navigate to the designated address -> and see our site.

Here’s what it looks like:
flaskrunning

Have fun building with Python, on Ubuntu, on Windows!

Bike with pizza tied to the back
Sometimes you gotta tie a pizza to your bike.

21 thoughts on “Python with Ubuntu on Windows”

  1. Thanks for the info.
    I have one more doubt. If I want to use self-complemented python with the python extension of visual code. How could I do it?
    I have done the following: python.pythonPath: “bash.exe -c python3”.
    However, it does not work for me.
    The purpose of doing this is to not duplicate packages in ubuntu and windows.

  2. Hi there, thanks for the tutorial!

    I have a doubt, I’m new in python. I will start a course next week, I saw in one screenshot on the last step that executed: python hello.py runserver and then you can browse it. But on my case I simple have and hello.py with a print(‘Hello word’) inside. When I execute the command I see the output in the console, not a webserver listen at the port 5000. How do I need to get the same behavior?

  3. Hey, thanks very much for writing this!

    The guide worked on my machine until I tried to run the shell script after installing virtualenvwrapper. I’d done the rest of the setup for Python 3 and the VIRTUALENVWRAPPER_PYTHON variable was pointed at Python 2.

    To get around this, I ran the following:
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

    I also added it to .bashrc and it now works every time I open Bash. Hopefully this helps if anyone else has the same problem.

  4. Timothy,

    Great guide. I appreciate the summaries here.

    I was hoping to connect the virtual environment to PyCharm, but the mkvirtualenv and virtualenv commands are not creating the bin/python folder or the python exectuable in each virtual env. Without the interpreter exe, I cannot choose the virtual env for PyCharm.

    Would you know why the virtual env do not contain an interpreter exe or the bin/python folder?

    1. hmmm that’s surprising. PyCharm probably has a built in way to deal with VirtaulEnvs were you able to figure it out? I’d be curious if you found a workaround.

    2. hey not sure if this is relevant – but without using the virtualenv wrapper my virtualenv folder has the /bin/python executable.

  5. Timothy,

    Great guide. I appreciate the summaries here.

    I was hoping to connect the virtual environment to PyCharm, but the mkvirtualenv and virtualenv commands are not creating the bin/python folder or the python exectuable in each virtual env. Without the interpreter exe, I cannot choose the virtual env for PyCharm.

    Would you know why the virtual env do not contain an interpreter exe or the bin/python folder?

    1. hmmm good question, were you able to get this to work? You were running PyCharm on windows trying to connect to the VirtualEnv in your Windows Subsystem on Linux environment?

  6. Apropos tying pizza to your bike.. problem is that bumps on road shake your pizza into mush. Sometimes better to use the string to tie the box so that it hangs down from the handlebars. Then, the string acts as a bit of a spring to reduce the shaking.

  7. I got to step 6 then got an error that a directory didn’t exist. Any clues to what the problem might be?

    Collecting six>=1.10.0 (from stevedore->virtualenvwrapper)
    Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
    Installing collected packages: pbr, six, stevedore, virtualenv-clone, virtualenv, virtualenvwrapper
    Successfully installed pbr-5.1.1 six-1.12.0 stevedore-1.30.0 virtualenv-16.1.0 virtualenv-clone-0.4.0 virtualenvwrapper-4.8.2
    btremaine@LAPTOP-TQQHUD7F:~/my_proj_folder$ export WORKON_HOME=~/Envs
    btremaine@LAPTOP-TQQHUD7F:~/my_proj_folder$ source /usr/local/bin/virtualenvwrapper.sh
    -bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory

  8. I’m not sure my first comment got posted. In trying to do the install I got an error at step #6. There was no directory. See print out below:
    =========================
    Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
    Installing collected packages: pbr, six, stevedore, virtualenv-clone, virtualenv, virtualenvwrapper
    Successfully installed pbr-5.1.1 six-1.12.0 stevedore-1.30.0 virtualenv-16.1.0 virtualenv-clone-0.4.0 virtualenvwrapper-4.8.2
    btremaine@LAPTOP-TQQHUD7F:~/my_proj_folder$ export WORKON_HOME=~/Envs
    btremaine@LAPTOP-TQQHUD7F:~/my_proj_folder$ source /usr/local/bin/virtualenvwrapper.sh
    -bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory

  9. I posted earlier on my problem with virtualenvwrapper. I found that $ source /usr/local/bin/virtualenvwrapper.sh gives a file not found.

    I searched for virtualenvwrapper.sh and found it under .local/bin. Then tried source /usr/.local/bin/virtualenvwrapper.sh and still got a file not found error.

    1. Had the same problem. I was able to run script by navigating to the folder /usr/.local/bin and run source ./virtualenvwrapper.sh

  10. Hey can anyone please help me?

    I followed the exact procedure described here. When executed ‘python filename.py runserver’, the server started. When I opened localhost (127.0.0.1:5000) on my browser it showed me can not reach this page.

Leave a Reply

Your email address will not be published. Required fields are marked *