Visual Studio IntelliCode. Try AI-assisted IntelliSense that learns from your code in C#, and supports your language of choice, by downloading our experimental extensions for Visual Studio 2017 version 15.9, Visual Studio 2019 Preview 1 and Visual Studio Code. Try AI-assisted IntelliSense that learns from your code. This snap was maintained by the Snapcrafters community, but will not be getting any further updates. Microsoft is now publishing an official snap of Visual Studio Code, you should migrate to it using these commands. Snap remove vscode snap install code -classic.
You can write F# in Visual Studio Code with the Ionide plugin to get a great cross-platform, lightweight Integrated Development Environment (IDE) experience with IntelliSense and basic code refactorings. Visit Ionide.io to learn more about the plugin.
To begin, ensure that you have F# and the Ionide plugin correctly installed.
Note
Ionide will generate .NET Framework F# projects, not dotnet core, which can have cross-platform compatibility issues. If you are running on Linux or OSX, a simpler way to get started is to use the command-line tools.
Creating your first project with Ionide
To create a new F# project, open Visual Studio Code in a new folder (you can name it whatever you like).
Next, open the command palette (View > Command Palette) and type the following:
This is powered by the FORGE project.
Note
If you don't see template options, try refreshing templates by running the following command in the Command Palette: >F#: Refresh Project Templates
.
Select 'F#: New Project' by hitting Enter. This takes you to the next step, which is for selecting a project template.
Pick the classlib
template and hit Enter.
Next, pick a directory to create the project in. If you leave it blank, it uses the current directory.
Finally, name your project in the final step. F# uses Pascal case for project names. This article uses ClassLibraryDemo
as the name. Once you've entered the name you want for your project, hit Enter.
If you followed the previous step, you should get the Visual Studio Code Workspace on the left-hand side to appear with the following:
- The F# project itself, underneath the
ClassLibraryDemo
folder. - The correct directory structure for adding packages via
Paket
. - A cross-platform build script with
FAKE
. - The
paket.exe
executable that can fetch packages and resolve dependencies for you. - A
.gitignore
file if you wish to add this project to Git-based source control.
Writing some code
Open the ClassLibraryDemo folder. You should see the following files:
ClassLibraryDemo.fs
, an F# implementation file with a class defined.ClassLibraryDemo.fsproj
, an F# project file used to build this project.Script.fsx
, an F# script file that loads the source file.paket.references
, a Paket file that specifies the project dependencies.
Open Script.fsx
, and add the following code at the end of it:
This function converts a word to a form of Pig Latin. The next step is to evaluate it using F# Interactive (FSI).
Highlight the entire function (it should be 11 lines long). Once it is highlighted, hold the Alt key and hit Enter. You'll notice a window pop up below, and it should show something like this:
This did three things:
- It started the FSI process.
- It sent the code you highlighted over the FSI process.
- The FSI process evaluated the code you sent over.
Because what you sent over was a function, you can now call that function with FSI! In the interactive window, type the following:
You should see the following result:
Now, let's try with a vowel as the first letter. Enter the following:
You should see the following result:
The function appears to be working as expected. Congratulations, you just wrote your first F# function in Visual Studio Code and evaluated it with FSI!
Note
As you may have noticed, the lines in FSI are terminated with ;;
. This is because FSI allows you to enter multiple lines. The ;;
at the end lets FSI know when the code is finished.
Explaining the code
If you're not sure about what the code is actually doing, here's a step-by-step.
As you can see, toPigLatin
is a function that takes a word as its input and converts it to a Pig-Latin representation of that word. The rules for this are as follows:
If the first character in a word starts with a vowel, add 'yay' to the end of the word. If it doesn't start with a vowel, move that first character to the end of the word and add 'ay' to it.
You may have noticed the following in FSI:
This states that toPigLatin
is a function that takes in a string
as input (called word
), and returns another string
. This is known as the type signature of the function, a fundamental piece of F# that's key to understanding F# code. You'll also notice this if you hover over the function in Visual Studio Code.
In the body of the function, you'll notice two distinct parts:
An inner function, called
isVowel
, that determines if a given character (c
) is a vowel by checking if it matches one of the provided patterns via Pattern Matching:An
if..then..else
expression that checks if the first character is a vowel, and constructs a return value out of the input characters based on if the first character was a vowel or not:
The flow of toPigLatin
is thus:
Check if the first character of the input word is a vowel. If it is, attach 'yay' to the end of the word. Otherwise, move that first character to the end of the word and add 'ay' to it.
![Ifort Ifort](https://www.hanselman.com/blog/content/binary/Windows-Live-Writer/97b35c87376a_CF10/Screen%20Shot%202017-05-31%20at%203.01.29%20PM_17b8d92f-b0b1-4f5c-a0a2-0052faf25f7b.png)
There's one final thing to notice about this: there's no explicit instruction to return from the function, unlike many other languages out there. This is because F# is Expression-based, and the last expression in the body of a function is the return value. Because if..then..else
is itself an expression, the body of the then
block or the body of the else
block will be returned depending on the input value.
Moving your script code into the implementation file
The previous sections in this article demonstrated a common first step in writing F# code: writing an initial function and executing it interactively with FSI. This is known as REPL-driven development, where REPL stands for 'Read-Evaluate-Print Loop'. It's a great way to experiment with functionality until you have something working.
The next step in REPL-driven development is to move working code into an F# implementation file. It can then be compiled by the F# compiler into an assembly that can be executed.
To begin, open ClassLibraryDemo.fs
. You'll notice that some code is already in there. Go ahead and delete the class definition, but make sure to leave the namespace
declaration at the top.
Next, create a new module
called PigLatin
and copy the toPigLatin
function into it as such:
Next, open the Script.fsx
file again, and delete the entire toPigLatin
function, but make sure to keep the following two lines in the file:
Select both lines of text and press Alt+Enter to execute these lines in FSI. These will load the contents of the Pig Latin library into the FSI process and open
the ClassLibraryDemo
namespace so that you have access to the functionality.
Next, in the FSI window, call the function with the PigLatin
module that you defined earlier:
Success! You get the same results as before, but now loaded from an F# implementation file. The major difference here is that F# source files are compiled into assemblies that can be executed anywhere, not just in FSI.
Summary
In this article, you've learned:
- How to set up Visual Studio Code with Ionide.
- How to create your first F# project with Ionide.
- How to use F# Scripting to write your first F# function in Ionide and then execute it in FSI.
- How to migrate your script code to F# source and then call that code from FSI.
You're now equipped to write much more F# code using Visual Studio Code and Ionide.
Troubleshooting
Here are a few ways you can troubleshoot certain problems that you might run into:
- To get the code editing features of Ionide, your F# files need to be saved to disk and inside of a folder that is open in the Visual Studio Code workspace.
- If you've made changes to your system or installed Ionide prerequisites with Visual Studio Code open, restart Visual Studio Code.
- Check that you can use the F# compiler and F# interactive from the command line without a fully-qualified path. You can do so by typing
fsc
in a command line for the F# compiler, andfsi
orfsharpi
for the Visual F# tools on Windows and Mono on Mac/Linux, respectively. - If you have invalid characters in your project directories, Ionide might not work. Rename your project directories if this is the case.
- If none of the Ionide commands are working, check your Visual Studio Code keybindings to see if you're overriding them by accident.
- If Ionide is broken on your machine and none of the above has fixed your problem, try removing the
ionide-fsharp
directory on your machine and reinstall the plugin suite.
Ionide is an open source project built and maintained by members of the F# community. Please report issues and feel free to contribute at the Ionide-VSCode: FSharp GitHub repository.
If you have an issue to report, please follow the instructions for getting logs to use when reporting an issue.
You can also ask for further help from the Ionide developers and F# community in the Ionide Gitter channel.
Next steps
To learn more about F# and the features of the language, check out Tour of F#.
I am using the following setup
- MacOS Mojave
- Python 3.7.1
- Visual Studio Code 1.30
- Pylint 2.2.2
- Django 2.1.4
I want to us linting to make my life a bit easier in visual studio code however, every import i have states 'unresolved import'. Even on default django imports (i.e. from django.db import models).
I presume it is because it is not seeing the virtual environment python files.
Everything works just fine but but it's starting to get annoying.
The interpreter choices i have are all system versions of python. It does not seem to see my virtual environment python at all (it is not in the same directory as my workspace, so that part makes sense).
If i setup the python.PythonPath in the settings.json file, it just ignores it and does not list my virtual environment path as an option. I also tried setting it up in my global python settings but it also does not show up.
Has anyone run into this issue and know a quick fix to get it working?
Thanks,jAC
6 Answers
In your workspace settings, you can set your python path like this:
If you have this code in your settings.json
file, delete it
This issue has already been opened on GitHub:https://github.com/Microsoft/vscode-python/issues/3840There are 2 very useful answers, by MagnuesBrzenk and SpenHouet.
The best solution for now is to create a .env file in your project root folder. Then add a PYTHONPATH to it like this:
and in your settings.json add
I have a different solution: my VSCode instance had picked up the virtualenv stored in .venv
, but was using the wrong Python binary. It was using .venv/bin/python3.7
; using the switcher in the blue status bar, I changed it to use .venv/bin/python
and all of my imports were resolved correctly. I don't know what VSCode is doing behind the scenes when I do this, nor do I understand why this was causing my problem, but for me this was a slightly simpler solution than editing my workspace settings. I hope it helps someone.
When I > reload window
that fixes.
ref: https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-452657892
Alternative way: use the command interface!
cmd/ctrl + shift + p
>Python: Select Interpreter
> choose the one with the packages you look for