I prefer using a Java-like organization with my projects. For instance, each class has a different .py file as well as class Main, which only contains main function.
Each time I make a change in one of my classes, I need to click on Main and run the code (Ctrl+Shift+F10) from there.
How can I define my Main class as my main() function just as in Eclipse?
You can create "run configurations" like in Eclipse.
The name of the thing in the dropdown is the run that you want and should not change between changing files. Clicking the triangle (or using alt-shift-X under the eclipse keymap) should run that configuration. In the dropdown you have the option of selecting whatever file you want to run.
Related
I am new to VS Code and I run unittest in VS Code for Python.
The checkmarks are for each method is shown only inside the beaker button but not inside the code editor.
I follow the steps below but nothing shows up inside the text editor.
To test your classes use the keyboard shortcut "Ctrl + shift + P" for windows or "cmd+shift+P" on Mac to bring up the command Palette.
Enter "Python: Configure Tests" and select unittest. You will then be prompted to select the directory of where your tests are located and finally you need to
choose "test_*.py" option because your file names should have the form test_.py
This will allow you to individually test methods without having to wait for all of them to be tested.
To test all methods and to view coverage do the following.
python will run your test file and tell you how many passed
I am trying to use the variable explorer in PyCharm.
I click on python console and I have a window for the variables.
However, the variables that I created in my code do not appear, only built in variables appear. (Under special variables, there is: builtins and sys)
Where can I find the variables that appear in my code?
The specific variable I'm looking for is a dictionary called "data_model".
It is created within the program and filled during runtime.
You can run the code in python console and then you will be able to see all the variables (along side builtin variables). By default it will only show builtin and sys variables and as you run the code in python console, it will show the variables.
The dictionary view is not great I think but in pandas dataframe you will be able to right click on variable and click on 'view as a dataframe' which puts the dataframe in a new window.
In Spyder I can run only a part of code without running everything. I know that in PyCharm I can click right mouse button and "Execute Selection in Console", but it will be new execution without values and variables which declared before this part of code.
So, very often I need to run only few last lines of my code, with parametres and options that I already have.
you can try to use the interactive interpreter while in debug mode.
I find it to be very useful when trying to run code snippets in the program.
view the screenshot below.
You can use the "Run cell" functionality and the cell will be executed in the Python Console (the same way it does when right clicking "Execute Selection in Console"). To enable that I am aware of two options :
In settings (Ctrl+Alt+S) install the "PyCharm cell mode" plugin.
Then use ## to create code sections.
https://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode
Create a new project in scientific mode (only available in PyCharm professional)
You can create code cells with #%%.
https://www.jetbrains.com/help/pycharm/matplotlib-support.html
In both cases, it creates cells that you can execute with the green "play" button like shown below :
I have download some mesh exporter script to learn how to write an export script in python for blender(2.6.3).
The script follows the standard register/unregister in order to register or unregister the script.
### REGISTER ###
def menu_func(self, context):
self.layout.operator(Export_objc.bl_idname, text="Objective-C Header (.h)")
def register():
bpy.utils.register_module(__name__)
bpy.types.INFO_MT_file_export.append(menu_func)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.INFO_MT_file_export.remove(menu_func)
###if __name__ == "__main__":
### register()
unregister()
The issue is that when I use runScript to run the script from the text editor(after changing it to unregister upon run) it removes the the script but leaves an unclickbale leftover in the export menu which I cannot remove.
If I run the register again, it will turn back the inactive menu option into a clickable exporter menu item but in addition it will add another copy of the menu item.
The reason I want to keep registering and unregistering is mostly because I want to make changes and test them out...
Maybe I should run the function directly without registering but even though now I have this in my export menu:
How do I remove these items and not have many versions of my script in the export menu(depending if I made changes), also should I just put the function instead of the register/unregister when I am fiddling with the script and trying thigns out?
Well I have found a workable way...
If you press 'F8' it will reload all plugins and remove the "dead" menu items.
That solves the multiple additions of the same addon.
So now if I want to change the addon and test it I do something like this:
Run script with unregister
Press F8
Run script with register
That is how I update the addon and there is the additional step of actually running it from the export/import menu.
If you have an easier way to test changes for the addon please let me know...
I am not 100% certain of the cause but it relates to running an addon script that adds a menu item within blender's text editor. Even blender's template scripts do the same thing.
I think the best solution is to use it like a real addon - that is save it to disk and enable/disable it in the addon preferences. You can either save it to the installed addons folder, within your user settings folder or create a folder and set the file path for scripts. You could also use the Install from File button in the addons preferences.
I've created a couple of modules that are used in a game I'm trying to write.
The modules themselves work fine (as part of the main program), but hang if I run them separately. Is there a way to make them not run/load up or perhaps instantly quit unless they are imported and run by the main program?
I'm very new to programming and make a lot of mistakes, so I constantly test run the code and at times forget to switch from the "module.py" tab to my "main.py" tab. It loads up the window and hangs, leaving no choice but to force-quit it from the task bar.
If you're not wanting the code in you modules.py to be run independently, why not place it inside a function which you call in main.py?
For example modules.py
def foo():
# code goes here
and in main.py
import modules
# when code from modules.py is required
modules.foo()
or you could have
from modules import foo
# when code from modules.py is required
foo()
-Thanks to #laurencevs for pointing out I merged both options :s