Directory issues within Pycharm (free version) & Perforce - python

Ok, so I'm looking to switch to PyCharm from PyScripter for OS independent development. I also wanted to mention that I'm using Perforce for version control.
So what I currently do is double click a .py for editing in Perforce, and PyScripter opens up and I edit to my hearts desire. I can click on an imported function, and it'll open up the corresponding .py file and bring me right to the function. Awesome.
So I have yet to be able to achieve that on PyCharm. I'm using the community version which should be just fine for what I want, which is just an editor with some python checking & built in console.
When I set the default .py program to use in Perforce to PyCharm, I click on the .py and PyCharm fires up. Good so far. But my problem arises when I try to "ctrl + click" a function or method. I get the "Cannot find declaration to go to." I import the associated class & file.
(Just an example, not actual code). So in Transportation.py I have "import Cars", which is a .py. I do Cars.NumberOfDoors() and I get the above error. My folder structure is:
Scripts (folder)
Population.py (General support script)
Citybudget.py (General support script)
MassTransit (folder)
Transportation.py
Cars.py
So question boils down to, is how do I properly setup the root to be the Scripts folder when I click on a file from Perforce? How do I set it up that it recognizes where it's at in the folder structure? So if I'm in the MassTransit it'll set the root as Scripts folder, and same for if I'm accessing the general support scripts like Population.py?

Go to
File --> Open
in Pycharm and select your Scripts(folder) and open it. Then the Pycharm will treat it as a project and you will be able to ctrl + click a function.

Related

AREPL and console showing different working directories (Visual Studio Code User) - why?

(I'm currently learning Python so I might be missing something obvious - sorry if this is the case)
I'm using Visual Studio Code and the AREPL extension (to get an idea of what will show up while I'm writing code) to learn Python coding
However, I encountered a weird error, where different working directories seem to be registered for AREPL and the console (sorry I'm new so my descriptions and wording might be inaccurate)
Specifically, if I type
cwd = os.getcwd()
print(cwd)
on the AREPL panel, it shows the folder containing the python file I'm working on as the working directory (C:\Users\XXXXXX\Coding\Python),
but when I actually run the code, the console shows the upper folder (directory?) (C:\Users\XXXXXX\Coding )
So when when I try to open different files in the C:\Users\XXXXXX\Coding\Python folder using a relative path (e.g. open("temp.txt", "r")) then it runs without error on the AREPL window and displays the expected results, but shows an error when I actually run it through console
This is also weird because I remember having used relative paths in the past without problem when I was working on a file in a different folder
FYI, I never changed any settings related to working directory or manually set them until now
I know that the obvious easy answer is to use an absolute path, but I'm just trying to figure out what might be causing the error to better learn about Python (or Visual Studio Code)
Thank you!
The reason is that the precise locations of the startup paths displayed by default on the two terminals are different.
In the default terminal of VS Code, the startup path displayed by default is the currently opened project folder. When a file is executed, it will automatically go to this file and then return to the project folder. This is convenient when executing any file in the project in this terminal, VS Code can go to it without reopening this terminal.
The "AREPL" terminal executes this python file, and it displays the parent folder of the executed file by default. When we switch other executable files, the "AREPL" terminal needs us to reopen this terminal.

Moving PyCharm projects folder and keeping reference to python interpreter

I'm using macOS 10.15.4 and PyCharm 2019.3.4
I currently have a folder inside the PyCharmProjects folder (that is automatically created when PyCharm first runs) where I keep projects for a class. I want to move this folder, which contains multiple other folders which each contain PyCharm projects, somewhere else on my computer (like the Desktop). The problem is if I move the folder (or even just a single PyCharm project) the next time I open the project in PyCharm, it says "Invalid python interpreter selected for the project." Now I can manually go into PyCharm preferences and point it to the new location I moved the folder so it can use the correct python interpreter. But this would be tedious to do for every single project I have. (And yes, every project I have uses its own interpreter and virtual environment.)
Is then a way I can move a folder containing multiple PyCharm projects without loosing the references to each of their respective python interpreters?
(Feel free to reword this question or the title.)
PyCharm uses configurations from your home directory. The docs say
macOS
Configuration
~/Library/Preferences/<PRODUCT><VERSION>
Caches
~/Library/Caches/<PRODUCT><VERSION>
Plugins
~/Library/Application Support/<PRODUCT><VERSION>
Logs
~/Library/Logs/<PRODUCT><VERSION>
where <PRODUCT> is PyCharm.
They are xml files. Among them also a list of configured interpreters. They are separate because they are available for all your projects to be chosen as the default interpreter. You'll have to edit the xml files.
For some reason PyCharm does not help with a moved virtualenv directory directly. (see comment from engineer: https://youtrack.jetbrains.com/issue/PY-32435#focus=Comments-27-3139072.0-0)
On the upside, everything else seems to work, except the path to the venv python. That one can be changed through preferences/project preferences/python interpreter. Just click on "add", "existing", and select the python compiler in your project folder.
Try not to select the ".virtualenvs" python compiler, as if you fumble like I did, it seems it's a bit of a pain to get rid of it and go back to the right one. If you do that, renaming the project folder once more gets you back to square one and you get to try again.
Note that I'm using MacOS. YMMV.

How to setup Python IDE make auto complete work?

I want to write IDAPython scripts and plugins. The problem is to get my Python IDE (PyCharm) to resolve the imports correctly and therefore get code completion correct.
Here is an example. I want that code completion works for IDAPython commands like:
ea = ScreenEA()
or
ea = idc.get_screen_ea()
Here is what I did. I downloaded IDAPython and put the source in my Python projects source folder.
I also copied the python folder from the IDA install directory into my Python projects source directory.
It turns out, that Pycharm cannot resolve the functions correctly.
How to setup Python IDE make auto complete work?
As long as the IDAPython files show up in you Pycharm project, or are in the same folder, you simply need to import the file you want to use.
So for your idc.get_screen_ea() example, as long as you have this at the top of your python file you are working on:
#import idc
Then you will get suggestions for anything involving idc.
Here is the method I took for PyCharm Windows version. First create an venv based project with the python version match your IDA python version.
File -> Settings -> Project -> Python Interpreter
Click setting icon, select Show All...
Select the interpreter you currently use, click Show paths for the selected interpreter
At the popup window, add your %IDA_INSTALL_PATH%\python
Then you can use PyCharm IDE features to write IDA python plugin.

How to change the default working directory in python

I have a quick question about the default working directly on python.
I am currently using the python 2.7. In this case the default working directly is the C:/Python27.
I want to change this permanently to another directory.
Even if we write down following on shell, the default working directory will go back to the original C:/Python27 automatically.
import os
os.chdir('a path')
Does any one know how to set up default directory permanently to "the path" which keeps the directory after the closing the shell?
This is a different question from how to change working directory just by the function temporarily.
Thank you so much for your help!!
The working directory of the python directory is the directory from which it was started. If from a console (cmd.exe) I do cd /some/dir and then start python, its working directory will be /some/dir.
If you want an interactive console started at a given directory you have some options:
Using a shortcut, you can change its Start in field, in the properties tab, to point to your target directory.
Use IPython instead, and add a startup script to the default profile so it always start at the target directory. IPython is an enhanced interactive with lots of useful features.
If you are running from a script and want to switch to the folder where you script is stored, you could use os.cd(os.path.dirname(__file__)).
If you are launching Python from the start menu of Windows, right click on the icon and select more -> file location.
Once there you can right click on the shortcut and select properties. From there you should be able to define the 'Start in:' location.

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories