VSCode Custom Python Debug Command - python

Description
I have been using VSCode for a while now. Currently, the Code Runner extension enables me to run Python scripts, using custom commands. However, I would like to know if VSCode is able to perform additional actions, compared to the Code Runner extension, which just runs my custom command pipenv run python -u {open_file_path}.
Goal
I currently use pipenv for my Python projects. I would like VSCode to launch a pipenv shell and execute a couple of commands to run my Python scripts.
Here are the steps I hope VSCode is able to automate when pressing F5 to start debugging.
Launch a pipenv shell if one is not already launched.
pipenv shell
Change directory to code source directory.
cd {project_root/src}
Run the script inside the pipenv shell.
python {open_file_path}
I have read that the launch.json is able to run preLaunchTask. It seems that it could resolve point one of launching pipenv shell. I am still trying to figure out how this works.
Any help or pointers is greatly appreciated.
Platform / Version
Platform: Windows 10
Python Version: 3.9.9
Pipenv Version: 2021.5.29
VSCode Version: 1.62.3
VSCode Microsft Python Extensions Enabled: Yes

Related

Cannot use python in VSCode

I simply want be able to execute python commands in VSCode.
I have already installed it by the marketplace and the main software on my computer.
Once I would have finished the python problem I am looking to install brownie, but I first need the ability to execute python commands in the VSCode terminal "pip install...".
Can you help me out?

python flask how to access CLI from terminal when running application from Pycharm

I am using python 2.7 with flask and using pycharm professional IDE, I am running the flask application using a virtual environment from inside pycharm.
When I open a terminal inside pycharm and use CLI commands, it works, and when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working, the app is still running all the time, and the command is excactly the same.
When I try to activate the same venv outside of the IDE i get permission issue, I assume that it has to do with the venv already being active inside the IDE.
What is the issue? I need to run the same virtual environment in order to use the CLI commands?
How can i access the CLI commands from outside of the IDE?
Thanks
When I try to activate the same venv outside of the IDE I get permission issue
Most likely the problem is all about permission access to the virtual environment's files. Check out access permissions and user:group ownership using ls -al (if you're on Mac or Linux), more info here:
https://linux.die.net/man/1/ls
https://linux.die.net/man/1/chmod
https://linux.die.net/man/1/chown
I assume that it has to do with the venv already being active inside the IDE
Definitely not, you can activate it as many times as you want.
I need to run the same virtual environment in order to use the CLI commands?
At least you have to have all the dependencies installed in your other environment (global or virtual) if you have plans to use one.
when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working
You'd better post a full error output so that we could check the actual error. Also what command are you trying to run?

Can't find the option to run Python file in web mode in Aptana studio 3

I am using Aptana studio 3 for Python. I created one demo.py file and typed some print statement. I tried to run this file with run as option but there was no sub options to run this file.
I have one doubt here whether the python interpreter is configured or not. Please help me to configure the python interpreter and run this file successfully.
You should be able to find out location of your Python interpreter with command which python in your terminal. I am not using Aptana myself, but after invoking mentioned command, you should set Python's path somewhere in Aptana's settings. If you are using virtual environment, be sure that you invoke which python command after activating virtual env.
Maybe this post can instruct you further.

Using linux console in pycharm

I'm new to pycharm, virtualenv, linux and git.
I've recently begun a journey of using djangoto make webapps. Before I used sublime to make scripts however now a more complex project management system such as pycarm was needed. I actually wanted to get a linux VM and go down that road but was advised that windows python IDE such as pycharm would be suitable
I recently learnt the importance of dependencies and how to use virtualenv. However in this tutorial, under the 'How do I use my shiny new virtual environment?' it starts using commands such as:
ls env
and
which python
Neither of which my pycharm console would understand.
I could use a console emulator such cmder to use the commands but then I would remove the convience of using the IDE's integrated one.
Should I upgrade to a linux VM ? Or can I install a package that allows me to use such commands in PyCharm.
As a bonus question, what are the commands in that tutorial ? are they linux commands? when ever i see $ .... is that the linux console ?
You can accomplish this using Vagrant: https://www.vagrantup.com/
You can use Vagrant and VirtualBox to setup a Linux VM (distro of your choice) and then install all of your Python dependencies in the VM. Once you have that setup, you can tell PyCharm to use the Python interpreter in your VM by following these steps:
Open the project settings dialog box in PyCharm.
Expand Project: (your project name) on the left side.
Click on Project Interpreter.
Click on the cog icon on the upper right side of the window and select Add Remote.
Click on the Vagrant radio button.
In the Vagrant Instance Folder box, select the directory your Vagrantfile is located in.
In the Vagrant Host URL box, make sure ssh://vagrant#127.0.0.1:2222 is specified.
Click OK.
Since Vagrant is compatible with Windows this solution should work for you. I have done it successfully using macOS and it works great. Good luck!
You might find this tutorial useful: https://developer.rackspace.com/blog/a-tutorial-on-application-development-using-vagrant-with-the-pycharm-ide/
I got this to work on Windows 10 with Anaconda Prompt. This terminal which comes with Anaconda, creates a "base" environment with a linux-like virtual machine and your Windows file system (C:\\) mounted to /c, and has bash installed with common Unix commands like cd, ls, chmod, echo, cat, ... Running programs from bash with access to environment variables is much nicer than Windows Powershell etc.
Now to get your Terminal in Pycharm to use Anaconda Prompt instead of cmd.exe, I followed this answer. After installing Anaconda and/or Anaconda Prompt, right-click -> Open File Location -> right-click the shortcut -> Properties -> copy file path. Then use your file path instead.
Conda is great for package environment management. Learn more about it here. For Django + Conda specifically, read here. You can also use pip to install from Python package indexes, github repos, and requirements.txt files instead. Unless you know how Anaconda Prompt works, I don't recommend creating your own environments from scratch. What worked for me was:
(base) C:\Users\wassadamo> conda create -n mynewenvironment --copy base
...
(base) C:\Users\wassadamo> conda activate mynewenvironment
(mynewenvironment) C:\Users\wassadamo> ls
folderA folderB file.txt
Works!
Whenever I try running conda deactivate to leave the base environment, my bash commands would stop working. So clone base as above.
Another tip: if you want to run shell scripts from Terminal within PyCharm with Anaconda Prompt this way, then execute them (e.g. "run.sh") on command line with
bash run.sh
I tried putting this on the first line of my run.sh
#!/usr/bin/bash
And running it with
./run.sh
But this had the effect of running it in an external Anaconda Prompt instance (add sleep, or some user input command to force it to wait and see for yourself). Explicitly running my .sh files with bash had the desired effect of running them in the same shell as I started them in PyCharm Terminal configured with Anaconda Prompt.

Is it possible to easily extract python run configuration (with additional path) from Pycharm?

I have a working Python project on my PC, which I am running from Pycharm.
It uses Pyroot (an interface to Root C++ library), whose C++ lib path I have added in Project Settings/Python Interpreter/Paths in Pycharm. It also needs to use the 2.7 Python interpreter, instead of 3., which is a default python in my terminal.
I want to run this project remotely on another desktop, so I need to be able to run it from terminal specifying the path to Root and the interpreter version.
Is there a way to easily extract from Pycharm the exact run command it is using when I'm running the code via run button?
Alternatively, if that's impossible, how should I specify the path to Root and the interpreter version when running from terminal?
I guess to best way is to create a virtualenv either in the terminal or in pycharm including the corrext python version 2.7 and install pyroot via pip into this virtualenv. Then you can simply ssh in the remote host, activate the venv and start your project from the terminal. Or you ssh into it with X-forwarding and start Pycharm itself from your client.
If you select the correct project and go to File > Settings, under the Project Settings you can see the Project Interpreter which tells you which interpreter is being used.
Hope this is what you are looking for.

Categories

Resources