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

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.

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

What is the expected behaviour in a virtualenv python?

Here is my problem, I am trying to make an application which copies data files during its setup. When i am doing pip install the setup copies a few files to a directory.
Now my question is, When inside a virtual environment, what is the behaviour that the customer expects- does he want all the created data files inside the virtual environment directory for each virtualenv or copy all the files into a common directory outside the virtual environment directory.
While running the application there will be new files that will be created and stored along these copied files. What is the ideal behaviour that is expected form a python virtualenv. common or isolated?
virtualenv is more for development, not for deployment. There are many scenarios to deploy Python app. but if you prefer virtualenv usage and you have common files, they can be anywhere IMHO, because virtualenv is not real isolation, it's only Python paths mangling/modification mechanism (not like "chroot"), so you decide where to place your common files, even /usr/share/my-app-1.0/datafiles/. Also, virtualenv is used to isolate binaries/scripts, but if data files are static, you can place them when you prefer.
In my opinion, that depends on application, which you create. Virtualenv is just way of running on same machine multiple applications with different dependencies. Data from applications is another thing.
When I would write web application, that will be single app on server, then I would use one directory.
On the other hand, when I would write GUI app, then things get different. If data is something that must be changed with every version, but end user does not touch it directly (e.g. some dictionaries, translation, etc) I would put it in dist-packages along the application package (see package data in setup.py).
On the other hand, if user can "touch" and use those files, then I would put them in users home directory. See How to find the real user home directory using python?

What to commit to source control in Eclipse Pydev

I've create a pydev project in eclipse.
At the top level of my workspace I can see these two files:
.project
.pydevproject
I can also see these in each of my subfolders that contain my actual projects.
At the top of my workspace there is also a
.metadata. folder.
What should I commit to source control?
Ie what can I delete and still be able to open the project with minimal effort (hopefully entirely automated regeneration of files)? If this was Visual Studios C++ project the answer would be to keep just the ".sln", "vcxproj" and "vcxproj.filters" because the "vs" folder and "suo" files will autogenerate on openning. I've tried to delete the ".metadata" folder, but after that nothing appears to load in my workspace.
Also, I am working with someone not using an IDE. What eclipse files do we need to update to keep in sync?
Disclaimer: I am not familiar with PyDev, just with Eclipse in general. You definitely should not check in the .metadata folder. That one is for your Eclipse workspace as a whole and contains your personal configuration. (That's why your workspace appeared empty after you deleted that folder.) In fact, you should not check in your workspace folder at all, but just the several project folders within it.
Whether to check in the .project files is sort of a matter of taste. Those contain project specific information and settings and with those its easier to import the project into Eclipse, but you can import the project without those, too, it's just a bit more work. If other developers are not using Eclipse, those are useless for them. In the worst case, your co-developers will delete those files from source control and when you update your project later, they are deleted on your end, too, messing up your project.
About deleting the files: Note that there is a difference between not checking files into version control and deleting them locally. So in short: Do not commit those files into version control, but don't delete them locally, either. Depending on what sort of version control you are using, you can set it to ignore those files.

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.

Locking on file locations in a large Python project

When a Python project gets big, certain code segments, such as utility functions, tend to be run from various locations:
from a __main__
from a django server process
from a test in a test suite
In each case the working directory for the python interpreter may be different and assuming the project spans over a sub-directory tree, the following line doesn't always work:
with open('some_file.xml','r') as my_xml:
It doesn't work because some_file.xml isn't always in your working directory. You need to be specific regarding the file's location, however, the project may be deployed in various environments so simply adding the directory to the open statement isn't a good solution.
What would be an elegant and efficient way to "lock on" the location of the file throughout the project?
using the following variable to get the directory of the project may help
__file__
How to make a python program path independent?

Categories