How to run standalone files in PyCharm - python

I'm doing small time project development using PyCharm. I use Pycharm for its intellisense features. As I develop each piece of code, I like to run it occasionally to test it. All I need at the point of development is to be able to run the file. However, when I right click and try to run a standalone file, PyCharm tries to be intelligent and shows me options to run my code with unit-tests and other fancy testing gimmicks. I don't want to deploy any testing framework at this point.
All I want is to be able to run any file as it is. But somehow, PyCharm is not allowing me to do that for every file.
I will appreciate if someone can provide a workaround for this. I'm using Python 273

You can simply choose the 'Run' entry in the context menu for the file in the Project view:
This will run the file exactly as if you had entered
python
from the command line, in the directory containing the file.
The first time you do this an entry is also added to the toolbar, which allows you to run the file with a single click
and provides options for customizing a range of options supplied automatically each time you run.

I just came across the same problem. It was because it had a class called TestClass in the file. I changed the name of the class and then I was able to run the file as normal.

Related

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

How do I create a custom RMB in Linux to execute python script

Background: Coming from windows, where I had created a couple of small C# programs I could execute from right mouse button on folders. Have since moved to Linux (completely new) and I would like to be able to do something along those lines. Also beginning to learn python hence the combo. Script will be used to manipulate the folders and files within the folder clicked.
So I guess I have three questions:
How do I add a custom RMB accessible from clicking on folders?
How do I get that to execute a python script? (googling got me as far as adding the following line at the top of the script might be a way to go: "#!/usr/bin/env python3", so maybe just point out the location of the script is enough and that line tells it to execute using python3)
How do I pass the folder name or folder location in as a parameter to the script and how to access it in the script?
*Edit...
Assuming the RMB could execute something like "python3 my_script.py 'something' 'something else'" I have figured out how to catch those somethings within my_script.py. So if I'm not way of on this I'm mostly interested in questions 1 and 2.

How to import a file into Spyder

To learn python, I'm attempting the infamous baby names exercise.
I'm having trouble getting going. How do I import the file babynames.py from the folder C:\Users\user1\Desktop\google-python-exercises\babynames?
The directory is set to cd C:\Users\user1\Desktop\google-python-exercises\babynames, but the commmand import babynames is invalid.
I searched around for an answer, but most of the Google results I've found solve much more complex variations of this simple example.
Thanks for your time!
You shouldn't be importing the file - the instructions want you to edit the babynames.py directly. What you'll need to do is (assuming you're using spyder due to the tag on your question):
find and open babynames.py from Spyder
read through the file, follow the instructions, and edit where it is marked with your code here
run the file to test it. If you need to enter args (eg. for the default babynames code you need to give the output file with --summaryfile <file>.html, write them in the Command line options box in the run configuration dialog in Spyder.
If you're using the command prompt instead, you will need to edit the file using any text editor, and to run it, enter a command like: python babynames.py --summaryfile some-file.txt where the working directory is C:\Users\user1\Desktop\google-python-exercises\babynames and python is on your path. (you may need to give a different command to run python itself from cmd - it will depend on your setup).

Edit file while executing in PyCharm

I am working on a project in PyCharm that involves extensive computations, with long runtimes.
I would like to do the following: I come up with a version of my code; then run it, then I edit the code some more; however, the run I started before still only uses the old version of the code (i.e. the snapshot at the point of running).
Is this possible in PyCharm?
I run my project by selecting the Run 'projectname' option from the Run menu.
I understand the run works by pre-compling the .py files to .pyc files stored in the __pycache__ folder. However, I don't know the following.
Will saving the file in PyCharm cause the .pyc files to be replaced by new versions? This is something I want to avoid since I want one run to only use one snapshot of the source tree, not multiple versions at different points of execution.
What if some python class is only needed, say, 20 minutes after the run has started. Will the .pyc file be created at the beginning of the run, or on-demand (where the corresponding .py file might already have changed)?
I use PyCharm in my classes. My experience is that the all the required code, including the imported modules, are compiled at runtime. If you change anything in that suite you need to start running from scratch for it to take effect.
I'm not a professional programmer so my experience is with small apps. I'd love to hear form an expert.

bootstrap script to set environments temporarily

I am trying to follow some simple instructions to use python for operating with QGIS from outside the application. Following the instructions in http://www.qgis.org/en/docs/pyqgis_developer_cookbook/intro.html#python-applications I am stocked where it says "These commands can be put into a bootstrap script that will take care of the startup". I never done this before, I have no idea how to create this type of script...
The commands I should bootstrap are:
set PATH=C:\qgispath;%PATH%
set PYTHONPATH=c:\qgispath\python
That is, these are ways to temporarily set system environment variables. However, having both python and (of course) the cmd prompt (my OS is Win7), I have no idea how to create the bootstrap script, and then how to call it from python, can anybody help me?
By a bootstrap script, the documentation probably means a batch file, which initializes the path variables and then runs your application.
For instance, if you have application stored as qgis_sample.py, bootstrap_example.bat would look something like:
set PATH=C:\qgispath;%PATH%
set PYTHONPATH=c:\qgispath\python
python <path_to_application>\qgis_sample.py
Hope this helps!
EDIT:
For running the bat file using a python script, refer to: Run a .bat file using python code

Categories