How do I activate a VSCode environment from ssh? - python

I am currently using VS Code on a server (through SSH). Everything works fine, and I installed Python packages and work with Python notebooks.
Now, I want to login to the server (not a problem) and run the Python code I created on VSCode, rather than executing it remotely.
My main issue is that I am not sure how to activate the Python environment (if there is one) that VSCode server's run so that the code can execute.
Is that possible?
I see I have a .vscode directory in my home directory, and there are package installation there.

After connecting vscode remotely, you can use it as a regular vscode, which is no different from running Python files locally:
install python
install pylance extension
choose correct interpreter
edit your code and run the python file.

Related

Module import errors with Virtual Environments and VSCode

I have created a virtual environment to install some Python modules. I am using mini-conda to manage and activate the environments.
One issue that I am facing is that the code runs fine when I run it through the terminal with the virtual environment activated.
However, the same code does not run when I use the "Run Code" button (Ctrl + Alt + N) in VSCode. It gives me Module Not Found Error.
How can I run the code from VSCode in the context of my virtual environment?
You should use the same interpreter in VSCode;
To do this you should use Python: Select Interpreter option;
CTRL+Shift+p(default key-bindings) and search for Python: Select Interpreter or search for it in the VSCode settings.
You must have the Code Runner extension installed because the Run Code option is provided by the Code Runner extension.
The triangle button for running code in the upper right corner has three options, among which Run Code is the way to run code provided by the Code Runner extension, while Run Python File and Debug Python File are the way to run and debug code provided by Microsoft's official extension Python.
Running the code with the Run Code option will run the code with the Code Runner extension. And Code Runner does not change the interpreter as you select a different interpreter in the Select Interpreter panel. It seems to always use the preferred interpreter from the system environment variables.
So although you are in a virtual environment and have some packages installed. But when you run code with Run Code, the interpreter is not the virtual environment of your choice. Although you have selected the virtual environment interpreter in the selection panel, this is only valid for Run Python File and Debug Python File provided by the Python extension.
So, run the code with the Run Python File option provided by the Python extension. Or install the packages you need for the interpreter environment currently used by Code Runner.
You can check the currently used interpreter with the following code
import sys
print(sys.executable)

Pycharm Run Linux Command before Console

Hi I am currently using pycharm to work on a python project. I am mostly running the code on a remote server which I have easy ssh access to. I currently have pycharm setup that I can easily upload/download files and use the ipython console with my environment. However, to fully be able to use the resources of the server I need to be able to run a linux command before running my python files or calling ipython so that I can have the resources I need to debug and whatnot. Is there a way to tell pycharm to run a specific command before starting my remote environment for the ipython console. (i.e instead of it doing conda activate environment; ipython it would do linux command; conda activate environment; ipython?
Thank you

Can't run Venv in VsCode

For the last 3 days, I have been trying to set up virtual Env on Vs Code for python with some luck but I have a few questions that I cant seem to find the answer to.
Does Vs Code have to run in WSL for me to use venv?
When I install venv on my device it doesn't seem to install a Scripts folder inside the vevn folder. Is this out dated information or am I installing it incorrectly. I am installing onto Documents folder inside my D: drive using python3 - m venv venv. The folder does install and does run in WSL mode but I am trying to run it in clear VsCode so I can use other add-ons such as AREPL that doesn't seem to like being ran in WSL.
For extra context I have oh-my-ZSH set up and using the ubuntu command line on my windows device. Any information will be helpful at this point because I am losing my mind.
venv folder in side D: drive
result
If you have the python extension installed you should be able to select your python interpreter at the bottom.
You should then be able to select the appropriate path
Run Set-ExecutionPolicy Unrestricted -scope process before activating virtual environment.
All the best
You don't have to create a virtual environment under WSL, it will work anywhere. But the reason you don't have a Scripts/ directory is because (I bet) you're running VS Code with git bash and that makes Python think you're running under Unix. In that case it creates a bin/ directory. That will also confuse VS Code because the extension thinks you're running under Windows.
I would either create a virtual environment using a Windows terminal like PowerShell or Command Prompt or use WSL2.

Is it possible to have VS Code use a Python environment that's inside a VM?

I've set up a Django (2.1.1) development environment using Vagrant and I've installed the Python extension for VS Code. I'm getting an error from pylint saying it can't import django.db, which makes sense since all of the Python modules are installed in the VM, and VS Code is using the Python environment on the host.
Does anyone know how (or even if) you can connect VS Code to the environment running inside of the VM so that linting and intellisense work?
This depends on how the VM and host are connected. If the user has permission to the interpreter you should be able to create a custom Python Environment in Visual Studio that uses that interpreter and its modules for intellisense.
This will get it to work from visual studio but in your code you may need to specify where you are importing the modules from. See Importing files from different folder
Take a look at the extension Remote - SSH.
I opened my Vagrant virtual environment with configuration the command:
ssh -p 2222 vagrant#127.0.0.1 -i /Users/sb0y/vagrant/ubuntu/.vagrant/machines/default/docker/private_key
Works perfectly.

ksh : ./bin/python2.7: cannot execute

Hi all I am working with IBM AIX remote server and cannot get the python inside the virtualenv to execute, though I have activated the virtualenv. I created the virtualenv on my mac machine with the all the dependencies installed required to run the project as I don't have the admin privileges on the AIX box and need to deploy and run my app on the server.
I was able to activate the virtual environment on the aix box by running the below command
. ./project_name/bin/activate
I tried running the file using the path to the python inside the virtualenv
/u/.../project_name/bin/python2.7 myfile.py
but go the error
ksh: ./bin/python2.7: cannot execute
Whenever i try to check the python that is currently being executed I get the system python path as a result
which python
/bin/python
I want it to execute the python inside the virtualenv and use the libraries that I have installed in there for the app I want to run. Any help would be appreciated.

Categories