This may seem trivial, and I admit that it is more a question relating to ease of access in VS Code.
My VS Code window shows "NO FOLDER OPENED". How do I fix this?
Up until the other day when I set VS Code as my default app for .py files (this is the problem), I could see any and all folders, sub-folders, and .py/.ipynb files on my Explorer tab. I am both new to Python and VS Code. I have tried these things to fix my problem:
Attempted to reset to all of microsoft's default app associations--the aforementioned files still open with VS Code.
Attempted to change each file's "Open with:" path (by accessing its properties)--it gives no other option, since PowerShell and CMD are not "apps".
Found this that got close but did not target my specific problem.
Found another article which refers to Windows 7--I didn't read it after seeing this.
I have done various other things, but I think you get the point; I'm at a loss!
This problem seems annoyingly easy to fix. I would rather see if there is some type of setting, in VS Code or Windows, that I am overlooking before searching for a complex work-around.
I'm also new to Stack overflow. I hope this is a "valid" question.
It's important to keep things organized even when you are new to coding. In VScode it is pretty simple to do that. I suggest you to make a folder for your projects and also for other purposes like practicing on examples.
Lets say you created a folder on desktop named Python, and you keep all your .py files in there, then you don't have to open each of the python files by double clicking it, it's a wrong practice. Instead, open VScode application from the start menu or desktop, then go to file > open folder > Python(the python folder you can create on your desktop). This way it opens a workspace(Folder) where you can work with different Python files or any other files.
With Ctrl key pressed, press O and then K. Then, select the folder where your python file is located.
Related
(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.
This is my first time coding a "project" (something more than solving exercises in single files). A number of my .py files have variables imported from a specific path. I also have a main "Run" file where I import things I've written in other files and execute the project as a whole.
Recently I've started working on this project on several different machines (home, work, laptop etc) and have just started learning how to use GitHub.
My question is, how do I deal with the fact that every time I open up my code on a different machine I need to go around changing all the paths to fit the new machine, and then change them back again when I'm home? I started writing a Run file for each location I work at so that my sys.path commands are ok with that machine, but it doesn't solve the problem of my other modules importing variables from specific paths that vary from machine to machine. Is there a way round this or is the problem in how I'm setting up the project itself?
In an ideal world it would all just work without me having to change something before I run, depending on the machine I'm working on, but I don't know if that's possible.
My current thoughts are whether there is some command I'm not aware of that can set variables inside a .py file from my main Run.py file - that way I can just have a run file for each machine.
Any suggestions are gladly taken! Whatever it is, it must be better than commenting back in the correct file path each time I open it on a different machine!
You should always use relative paths, not static which I assume you have got.
Assuming your in an index file and you need to access images folder, you probably have something like /users/username/project/images/image.png
Instead you want something like ../images/image.png, this tells your index file to go backwards one folder to say the root of the project, then proceed into our images folder etc.
Relative paths mean you create a path from where your file exists, and not an entire path from ground up.
You need to identify all your setting that are hardcoded in your project, and make them relative to your current workspace. This is a common problem.
Also if you are using python, make sure that you do not hardcode the path to files in string, but instead that you use the proper system api:
os.path.join('file','to','your','ressource')
that will be computed differently if you are on unix or windows.
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.
Basically I need to password protect a folder, but I've looked around and no software really fits what we need. I have an idea for how I'd go about it, but I can't find any way to check inside python if a folder is currently open in explorer, or watch for a folder being closed. Watchdog and Watcher seem to only be reporting information about files, and if a folder has been created/deleted. Any ideas about how to do this?
I've tried walking through open windows and using their titles to get an idea of what's open, but this doesn't work if a user is inside of a subdirectory. I was also thinking of hooking into explorer with a shell extension, but I have zero clue of where to start with that.
pss' was right. Attempting to rename the file and testing the exception on a set interval does work if a user has a file open. It doesn't work if they just have it open in windows explorer, but that actually doesn't matter now that I think of it. Comes with the added benefit of closing windows the user left open and forgot about.
I have written a python script with a Tkinter GUI. I would like to create a desktop icon that will execute this script so that the end-user (not myself) will be able to double-click the icon and have the GUI load, rather than 'run' the script from the terminal or python shell and then have to F5 from there.
Is there a way to do this? I have googled many arrangements of my question but most answers seem to be normal python scripts, not ones which are Tkinter based.
I am using a Raspberry Pi with Wheezy and Python 2.7
Thanks in advance.
I create executables(icons that I click to start the programs I write) using 'py2exe'. I use windows 7 and I am not sure if it would work for you with Raspberry Pi, but a google search may clear that up for you. I will provide the link below. Basically you end up with a folder with the executable(icon) and also some files, without which the executable won't work. It's unfortunate that you get this extra 'baggage', but it's still handy and the best solution I have come across. I don't think there is a much better way, but I am not 100% on that. Interestingly, I found that you could delete most of these baggage files and the executable would still work, it was trial and error, but it didn't take long. If I want to send the folder to someone, I zip it first.
py2exe is here
If you need a 64 bit binary you can get it here, along with, actually, pretty much every other version. get py2exe here also
Besides creating executable file other option is create simple .bat file:
Open notepad
Enter "C:\ProgramData\Anaconda3\python.exe" "C:\Users\Your ID\script.py"
First part is path to python.exe, second to your python script
save file as .bat file, ex. "open_program.bat"
Now simply double click on saved .bat file icon should open your script.
I like using cx-freeze to convert python scripts to exe.
There is very easy to follow documentation to do this. In short, you create a setup.py script that specifies what libraries and packages you want to include or exclude in your application.
You can then run the build in the console and it will create a build folder, in which will be an Application File. You can create a Desktop short cut using send to and selecting desktop.
Documentation link
You can save the script as a .pyw file so the user can click on the file and the GUi would open