In the PyCharm to debug any Python file, following configuration can be added.
Go to Run -> Edit Configurations and click on +
enter code hereSelect Python
Give some name
Provide Script: ->
Provide Script parameters: -> <>
Provide Environment variables: -> <>
Provide Python interpreter: -> <>
Provide Working directory: -> <>
So I wanted to know how to add equivalent configuration on VS code launch.json
So far I have got to know below parameters in VS code and remainig parameters I am trying to understand.
Provide Script parameters:
Provide Python interpreter:
Thanks a lot in advance.
As mentioned in the comments, what you said comes from the docs. You can edit your launch.json file to set it:
Provide Script parameters
args
Specifies arguments to pass to the Python program. Each element of the argument string that's separated by a space should be contained within quotes, for example:
"args": ["--quiet", "--norepeat", "--port", "1593"],
Provide Environment variables
env
Sets optional environment variables for the debugger process beyond system environment variables, which the debugger always inherits. The values for these variables must be entered as strings.
Provide Python interpreter
Python
The full path that points to the Python interpreter to be used for debugging.
If not specified, this setting defaults to the interpreter selected for your workspace, which is equivalent to using the value ${command:python.interpreterPath}. To use a different interpreter, specify its path instead in the python property of a debug configuration.
Provide Working directory
cwd
Specifies the current working directory for the debugger, which is the base folder for any relative paths used in code. If omitted, defaults to ${workspaceFolder} (the folder open in VS Code).
Related
So I tried adding R to the path on windows 10 (that is supposedly easy).
System Properties -> Environment variables -> Edit -> new: copy and paste: "C:\Program Files\R\R-3.5.0\bin\x64"
Now the thing is, Powershell just refuses to start the R environment when I type in R. R.exe works apparently. Rgui works as well. Is R a reserved letter in powershell or something? It also seems to repeat the previous command sometimes but that doesn't really seem completely consistent either.
(I put this entry on top of the list of the path and restarted the pc already)
when entering get-alias r I got the following result, so yes "r" is already taken ...
CommandType Name Version Source
----------- ---- ------- ------
Alias r -> Invoke-History
PS: you could remove that alias with remove-item alias:\r from your current powershell session and test if "r" then starts "R.exe". if that works for you, you could edit your profile to remove the alias "r -> Invoke-History" from every new session.
To generalize Guenther Schmitz' helpful answer:
PowerShell has several types of commands, whose precedence is:
Alias
Function
Cmdlet
External application
Note that name resolution is always case-insensitive, so that both r and R refer to the same command.
That is, before R resolves to R.exe, it is not only an r alias that could get in the way, but potentially also a function or a cmdlet (though the latter case is hypothetical, because well-behaved cmdlets follow a <Verb>-<Noun> naming pattern).
Note that built-in aliases shadowing external programs, especially standard ones, is problematic, and in the context of PowerShell Core a discussion is underway about whether to remove all built-in aliases and make them opt-in only - see this GitHub issue.
To see what a given name resolves to, use the Get-Command cmdlet:
# See what R resolves to
Get-Command R
# See ALL commands that R *can* resolve to, with the EFFECTIVE one listed first:
Get-Command -All R
Choices for unambiguously targeting R.exe:
(As you already know) If its folder is in one of the folders listed in environment variable $env:PATH, append .exe - i.e., use the filename extension explicitly:
R.exe ...
Use R.exe's full path (note the need for & for invocation, because the path needs quoting):
& "C:\Program Files\R\R-3.5.0\bin\x64\R.exe" ...
(For the sake of completeness; this is the cumbersome equivalent of using just R.exe): Use Get-Command -Type Application to locate the executable:
& (Get-Command -Type Application R) ...
Alternatively, as stated in Guenther's answer, you could add Remove-Alias r to your PowerShell $PROFILE file in order to remove the built-in alias on session startup, which then allows you to just use r to start R.exe.
Run the following code in your console to install the R package. This code will automatically add R to your os PATH.
sudo apt-get install littler
PyCharm allows customization of the Python console. By default it adds WORKING_DIR_AND_PYTHON_PATHS to the sys.path:
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS,WORKING_DIR_AND_PYTHON_PATHS + '/..', FILE_DIR])
Is there any variable for the "current file directory" (not current working directory)? This is needed to make relative paths work to other modules in same directory.
This can be done (using option 2 below) but not using the GUI shown in the question (option 1). It must be noticed there are 2 very different ways to launch the Console inside PyCharm.
1. As shown in the question by going to File > Settings > Build, Execution, Deployment > Console > Python Console.
2. Or using the Run/Debug configuration at Run > Edit Configurations.
What the GUI does in the 2 cases is very different
1. In the first case, the IDE simply calls the OS shell with the Python interpreter as first argument and the path to the Console plugin as second argument.
C:\path_to_venv\Scripts\python.exe
"C:\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py"
--mode=client --port=12345
There is one single variable of IDE magic to this (see also the comments in this answer):
Console. Python Console
The WORKING_DIR_AND_PYTHON_PATHS variable is hardcoded in PyCharm. It displays two paths: the project root and the working directory.
This means the IDE does not expose any other "magic variable" that would allow to retrieve the file before or after the Interpreter/Console is being called. Neither Python nor the OS have any way of knowing at this point what file/module you want to use, the only way would be hardcoding the file path as an environment variable (but this doesn't solve anything, because you would have to change the path every time you change file.)
2. The second option, does allow to transparently pass the module/file you currently have opened in the editor when you call the Console.
Basically by creating a run/configuration "template" using a using the "FileDir macro" whenever you run the debugger on any module opened in the editor a "temporary configuration" is created for that module that allows to retrieve the macro value from sys.argv. In this case the file is chosen by the IDE on-the-fly and the macro passes the path along with it.
C:\path_to_venv\Scripts\python.exe
"C:\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevd.py"
--multiproc --qt-support=auto --client 127.0.0.1 --port 12345
--file C:/path_to_module/teste2.py C:\path_to_module
This 2nd option is how the Console is supposed to be used in PyCharm to get the functionality in the question, as shown in the screenshot.
When running Pylint on my file I am getting the following message.
refactor (R0915, too-many-statements, function) Too many statements (95/50)
I want to set the number of statements a function can have to 100 instead of 50 in order avoid the above message from Pylint.
Pylint works based on configured settings which are default PEP 8 standards. Now, if customizing them is good or bad can be taken for another discussion, as they are kept like that for a reason. For example, if you have a method with more than 50 lines of code, it simply means that you are increasing the cyclomatic-cognitive complexities as well as making it difficult to unit test and get coverage.
OK, arguments aside, I think the following approach can help you customize the linting rule.
Go to your Python site-packages directory (it might be inside the Python installation Libs folder or in your virtual environment.
For example, D:\Python37\Lib\site-packages
Open a command line here, and navigate to the Pylint directory. Execute the configuration generator like
lint.py --generate-rcfile > custom_standard.rc
Now you will have a file named custom_standard.rc in the folder. Let’s copy it to some place around your project, say, D:\lint_config\custom_standard.rc.
Open the configuration file. You can see a setting for most of the rules. Now, for your question of number of statements inside a method, find the setting called
max-statements=50
Change it to:
max-statements=100
Save the configuration file. Now when you run the Pylint executable, use the option --rcfile to specify your custom configuration:
pylint --rcfile=D:\lint_config\custom_standard.rc prject_dir
If you want to integrate this with your IDE like PyCharm there are plugins which allows configuring the same.
But again!, it is not a good decision to change the PEP 8 :-)
Trying to run the wordcount.py example from the data-flow quickstart example via pycharm, and I ran into an issue when parsing the command line arguments that contain environment variables.
When I set the environment variables and run the script in the terminal with the same paramaters it works just fine:
python wordcount.py --input gs://dataflow-samples/shakespeare/kinglear.txt --output gs://%BUCKET%/wordcount/outputs --runner DataflowRunner --project %PROJECT% --temp_location gs://%BUCKET%/tmp/
I tried the following variations of parsing the environment variables - none worked: ${ENV}, $ENV$, %ENV%.
I am working on Windows 10, PyCharm version 2019.3.1.
When you pass environment variables as values for the parameters (e.g., --project, --temp_location) to Pycharm configuration, it takes those variables as "string" values instead of their real values which you set on the first screenshot "User environment variables". I did a quick search for many related threads on StackOverflow but not find a solution so I came up with my own one, that is, using your current settings and replacing the values after parsing arguments in your code:
# Args parsing here
if args.project == "${PROJECT}":
args.project = os.environ.get("PROJECT")
args.temp_location = args.temp_location.replace("${BUCKET}", os.environ.get("BUCKET"))
However, I think you should consider using it when
you work with PyCharm more frequently than Terminal.
there are many arguments referring to environment variables and you have to change their values for different settings.
Hope Pycharm will support this feature soon.
I'm attempting to run python on a system that doesn't allow me to set environment variables. Is there a commandline flag to python that will set PYTHONHOME? I looked here: http://docs.python.org/release/2.3.5/inst/search-path.html but didn't see anything.
So, hopefully something like this:
python -magical_path_flag /my/python/install test.py
EDIT
Thanks for the responses everyone. I'm embarrassed to say I actually meant PYTHONHOME, not PYTHONPATH. (That's what I deserve for asking a question at 1:30 AM.) I've edited my quesiton.
Here's some more info. I'm trying to get python running on Android. I can run python -V no problem, but if I try and execute a script, I get:
I/ControlActivity(18340): Could not find platform independent libraries <prefix>
I/ControlActivity(18340): Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Unfortunately when using the ProcessBuilder and changing the environment variables on Android, it says that they're not modifiable and throws an exception. I'm able to pass all the command line flags I want, so I was hoping I could set PYTHONHOME that way.
I've tried creating a wrapping shell script which exports PYTHONHOME and then calls python but that didn't work. (Got the same error as before.)
Thanks,
Gabe
You could simply set it in your script -- sys.path is a regular, modifiable list. Something like:
import sys
sys.path.append("/path/to/libraries")
should do the trick
In UNIXy shells, you can set an environment variable just for the duration of one command by prepending the command with the environment variable setting:
$ PYTHONPATH=/my/python/install python test.py
If none of the other answers suit you, you can write a wrapper script that temporarily sets the environment variable then exec's your other script. For example:
python mywrapper.py -magical_path_flag /my/python/install test.py