PyCharm Run/Debug configuration, do I really need one per file/tab? - python

I just got started with python and Im using PyCharm Build #PC-201.8743.11. I have 2 files in my project and Im using the Pipenv interpreter.
Each file has its own working Run/Debug configuration. Whats driving me mad is that if I switch file/tab I also have to manually switch Run/Debug configuration.
Is there no way to use one Run/Debug configuration per project or alternatively have the configuration locked per file so it switches the Run/Debug configuration as I change tabs?
Thanks
Sahand

The Run/Debug configuration doesn't follow which file you have open because generally within a project you will only have one (or not many) entry points, while the rest of your files are modules/libraries/helpers/whatever you want to call them.
If your two files are unrelated, then it sounds like you should have two different projects for them, in which case each project would have a Run/Debug configuration. If they are related, but are just two different entry points, then unfortunately you will have to make sure you are running with the correct Configuration each time you want to run. Depending on the complexity of your setup, this can be as simple as right-clicking in the file itself and selecting "Run <filename>".

Related

Can I set working directory in pycharm run/debug configuration witch something like $FileDir$?

I am trying to set my Pycharm run/debug configuration for robot framework tests. I would like to create configuration, where I can run any robot file in any directory with run/debug button.
$FileName$ works when set to Parameters field
But the working directory works only when the path is real. I've tried $FileDir$ and $FilePath$. None of those worked.
Note: I know about File->Settings->External Tools option, but I believe, there is also way via run/debug configuration
Ok, so setting it to directory of script worked. So If you select script in first field, then this folder is automatically set for you. It worked for me like this.

What are Django configurations?

I'm setting up a Django project for the first time using Pycharm, and I'm trying to figure out what the configurations do.
I have looked online at the documentation, but it seems to be more focused towards people who already know what configurations do.
I'm assuming it is used to set up my localhost, however I'm unsure. Any help would be greatly appreciated!
In an informal way, we can define PyCharm configurations as a comfortable way to start a new process.
Suppose that you have a Python script called main.py. If you have to run it via command line you will type
python main.py
In the same way, PyCharm offers the possibility to run this script using configuration. It's sufficient create a new Python configuration and setup the script path (location of main.py) and the relative python interpreter.
In fact, can happen that some scripts must be run using a virtualenv or a specific enviroments. For this reason, at the creation of PyCharm configuration we need to specify also python interpreter.
Configurations are not used just to start python scripts, but it's possible to create different types.
As you pointed out in your question, it's possible to create configurations also for Django.
A typical usage in this case is to create a configuration to start the runserver.
But this is not all, because it's possible create configurations also for django commands, for migrations and also for Unit Test.
Pycharm configurations are useful because in this way, no need to setup every time all environment to start a process, but you need just to create configuration and save it. You can use it whenever you want.
Last but not least, using configurations allow you to debug your code easily.
Configurations can be started also in debug mode.
PyCharm documentation
When you run your code/project in PyCharm, maybe you need to specify some set of environment variables, the entrypoint is a particular script in your project, maybe there's some other script you always need to run first before you can run your project, or lots of other fiddly things that are a pain to type in every time.
The purpose of PyCharm "Run Configurations" is to let you save all these kind of things into a single method of running your project with a single click or shortcut.
Django Configurations are a type of Run Configurations with a few extra Django-specific config options thrown in. The default settings in the Run Configuration are sensible (running on localhost:8000), although you will need to make sure you specify the location of your Django Settings File.
How I config Run/Debug Configurations in Pycharm Community:
Add a Python Configuration
Script path: "path to manage.py"
Parameters: runserver "host":"port" "option like '--noreload', '--nothreading'"
Python interpreter: "path to venv"
Now you are able to run with Shift + F10
In order to pycharm general setting left-top File>Setting
screenshot..
enter image description here
Python Setting....
Open a project or import
After open or import you view project name option in rigth-top bar like this
enter image description here
on click you can view configuration option. click it and view this form for all python or other environment setting.
enter image description here

What do I need to do to get Django Unit Tests working again in PyCharm after renaming a project and its directory?

I have several projects open in one window, some of which are different branches of the same project. I decided to rename the directory and project for one of these branches in PyCharm using the Refactor option on the project. After doing so, the Django Unit Tests Run Configuration I had working no longer works. It is still trying to run the tests from the old directory name.
I have not found anywhere in the Run Configuration to specify the directory. The working directory, yes, but that doesn't fix it. I tried changing all instances of the old path to the new path in the workflow.xml file in the root project (which is where the Run Configuration was stored), but that didn't help, and may have messed up other configurations.
Does anyone know where to change the directory for Django tests in PyCharm?
The Django project root setting does not get updated when you use the Refactor / Rename option, so you'll have to update it yourself. Go to Preferences -> Languages & Frameworks -> Django. Select the project of interest. Just below the Enable Django Support checkbox is the Django project root: setting. Click the folder, navigate to your project root, and click Apply or OK.
(Note that while there are other questions with the same or similar answers, the questions being asked are different enough that neither Google searches nor SO searches found them, thus I chose to document the answer in a way that someone else can find the answer.)

Opening various main.py files in Eclipse without creating a new project

I have a python project developed in eclipse. Independantly there are various data directories in various locations and it is desirable to operate on each with a different main.py that imports the python project. If I store a main.py with my data, how can I open it in eclipse and run the debugger?
I can run ipython in each directory without a problem; but, I am not clear on how to move between different main.py files within eclipse. The run configuration does allow you to go outside the project and I would prefer not having to change this each time anyway. Any insight would be great!
You could do this by setting up each main.py + data directory as its own Eclipse project, each with a custom run/debug configuration. You do have extra projects, but they don't require any ongoing maintenance.
After the initial setup, you continue work on the real python project and debugging any given data directory consists of selecting the appropriate project and invoking the debugger. Not maximally elegant, but not bad.
If you're truly loath to make new projects, you could bring the main.py files into the project and store the location of the associated data directory inside each of them. Each main begins by changing to the data directory, and then runs as usual. In eclipse you set up multiple run configurations, one for each main.py which you can then run without any extra tinkering.
It is inelegant and fragile to keep the data directory information inside each main.py, but maybe better for your purposes.

eclipse: Run/Debug current file

In search of a Python debugger I stumbled upon Aptana, which is based on eclipse.
Often, I want to debug a single python script. However, Aptana won't let me run/debug the currently opened file directly.
Instead, it requires me to create a debug/run configuration for each file I would like to run/debug. Alternatively I could create a Python project in Aptana.
But: I don't want to. I just want to be able to run or debug the currently opened file. This way I would like to debug my scripts without being forced to create a project first (for each single script!).
Can it be that hard?
This is because Aptana/Eclipse doesn't "realize" that the file you opened should be debugged using the Python debugger as it's not associated with a Python project/perspective (there's a lot of environment setup when a project is created in Aptana/Eclipse).
The simplest solution, IMO, would be to create a simple sandbox Python project and just stick your files in there to run/debug. Aptana should then realize you're dealing with Python and start running the Python debugger without setup (that's my experience w/ PyDev in Eclipse, at any rate).
See: Run External Python Programs with Eclipse PyDev (note: you really need a project, but your file doesn't need to be in it).

Categories