Getting started with Python
We provide this guide in order to help our users learn all they need to start developing their Python applications at SourceLair.
This guide assumes that you already have:
Here we will focus mainly on how to develop command-line Python applications on SourceLair. If you are interested into learning how to develop Python web applications at SourceLair, take a look at our our Django guide.
Creating a new Generic project will provide you with everything you need to get started. The only thing you have to do after that to get started is create your first Python file, by adding the
If you would like a more complete example to get started with Python on SourceLair, visit this URL to create your project https://lair.io/stacks/python.
The easiest way to install a dependency for your project is by running
pip install mypackage (e.g.
pip install requests) in your terminal.
The best way to install the dependencies of your Python project though is by creating a
requirements.txt, where all your dependencies will be listed, and then running the following command:
pip install -r requirements.txt
SourceLair provides you with intelligent as-you-type auto complete for your Python files. This feature is enabled by default; whenever you type, when editing a Python file, SourceLair will display code suggestions for you that save you time from typing.
To disable the as-you-type auto complete open up your Control Panel, navigate to your Editor Settings and just uncheck the Show suggestions on keystroke option. To display suggestions manually afterwards just hit Ctrl + Space when you need them.
Our editor will automatically check your Python file for syntax errors when you save it and display all errors and warning in the left side of the editor, next to the line number. This way you can produce better and safer code, since you do not have to run your application before correcting your mistakes.
If you find this feature too intrusive for you, just navigate to your Editor Settings and switch the Python linting option off.
Choosing default Python version
SourceLair ships with Python 2.7 (which is the default) and Python 3.4 pre-installed.
To use Python 3 without switching the default Python version just run
python3 to spawn the Python 3 shell or
python3 path/to/my/file.py to run it using Python 3. You can also use the
pip3 command to install Python packages for Python 3.
Defaulting to Python 3
To default your project to Python 3 just paste and run the following command in your terminal:
mkdir -p /mnt/data/.local/bin && ln -fs $(which python3) /mnt/data/.local/bin/python && ln -fs $(which pip3) /mnt/data/.local/bin/pip
Defaulting back to Python 2
To default your project back to Python 2 just paste and run the following command in your terminal:
mkdir -p /mnt/data/.local/bin && ln -fs $(which python2) /mnt/data/.local/bin/python && ln -fs $(which pip2) /mnt/data/.local/bin/pi
More information about choosing your Python version can be found at http://help.sourcelair.com/runtime/python-version/.
Tips for Python
Last we would like to share with you some great tips that can make Python development much more pleasant:
- By pressing Alt and clicking on a class or method name, it will open up the file where this class or method is defined and scroll you down to this position
*.pycin its own line in
.gitignore, to avoid committing Python bytecode files into Git
- Add the following entries in your
[*]section, to facilitate collaboration in Python projects:
indent_style = spaceand
indent_size = 4to standardize indentation to 4 spaces
max_line_length = 80to put a ruler in your editor and know when a line of code is too big
- Do everything from your keyboard with the Command Palette: Press Ctrl + Shift + P on your PC or Cmd + Shift + P on your Mac
- Open files rapid-fast from your keyboard: Press Ctrl + Shift + O on your PC or Cmd + Shift + O on your Mac
So that was it! Now you are ready to start developing your Python apps in SourceLair.
If you have any questions or run into any trouble remember to use the
Help: Contact us command from the Command Palette.