Visual Studio Code - setting it up for multiple languages - python

I've just downloaded VSC, because I wanted to edit all my code in one editor (I'm writing in C, Python and HTML). On my computer I have a folder (Programming), in which I have 3 sub-folders (C, Python, HTML). So when I started up VSC I opened this Programming folder, and also downloaded the necessary extensions for python and C in VSC.
Here comes my problem: if I run for example a .py which generates a .txt file, it's not generated in the same location as the .py file is, however in the main folder (programming). Also this issue occurs, when I build an .exe from a .c, it also builds in the main folder and not where my .c is.
Thank you for your help!

The default cwd(current working directory) is the folder you open, like you can see:
Even the script_2.py is under VSCODE\Python\Too\b_folder, its cwd is still under the folder VSCODE and not the specific project.
We can change cwd by the method os.chdir() in the file:
OR the second way, add "python.terminal.executeInFileDir": true, in settings.json
These are some workarounds, and it's still recommended that you turn to the specific project and go on development.

Related

Data directory not found in VCcode running Python code on Mac, but it exists and works perfectly fine in PyCharm

I'm trying to run some code on VSCode, as I'm shifting over from PyCharm and Vim to utilise VSCode as a general purpose editor for a variety of programming languages.
I'm currently trying to make some code work, which should have been quite straight forward given it is a project in my PyCharm IDE and I have been able to run it there.
When I open up my VScode workspace, and try and run the code, it cannot find a directory that obviouslly exists:
So I'm getting this error:
Exception has occurred: FileNotFoundError
data/connectivity/weights.txt not found.
And, my code is also pretty straight forward, its something like this:
SC = np.loadtxt(join("data/connectivity", "weights.txt"))
numpy is loaded in as np. and os.path.join is loaded in as join.
The directory data/ is in the same parent directory as the code/script I'm running.
I have no idea why it isn't recognising the path, or the folder.
**NOTE: **
To add, when I run my code in jupyter notebook in VSCode, it runs smoothly and recognises the path.
The jupyter notebook is also in the same parent directory as the data/ directory. So there is no visible difference between the .py and .ipynb files regarding code and relative position to the data/ directory.
Any help would be much appreciated. There is probably a simple solution that I'm failing to catch onto at the moment.
Thanks,
Boki
I've tried changing the directory to an absolute path on my Mac, which didn't work.
I also tried--though probably not so usefully--adding __init__py files to each of the subdirectories, but as they are not modules, this was sort of pointless, I was just trying to be exhaustive.
As I mention above, code works perfectly when I run the same code as a .ipynb, or when I run it in my old IDE: PyCharm
This is because the current directory of the terminal is different when vscode and pycharm run files.
By default, vscode executes files in the workspace, and pycharm executes files in the folder where the files are run.
You can search Terminal: Execute In File Dir in settings and check it.
This will be consistent with pycharm.
Of course, the default setting of vscode is also beneficial. When encountering multi-level directories, it is more convenient to import files.

Convert a visual studio python project to one executable file

I have finished my python project and now want to transfer the project into one file, so a user can just double click it and doesn't have to compile it first.
Therefore, I wanted to know if this is possible with python.
I 've read that you can convert a single python script into an executable file using pyinstaller. But in my case I have many files in different folders and I want them to be include in the executable file because otherwise the programm doesnt work.
I also tried this via the auto-pyto-exe converter (https://github.com/brentvollebregt/auto-py-to-exe) but I didnt got the results I wanted.
Therefore, my question is, how can I convert my visual studio python-project with many different files and folders into one executable file, so a user can just double click the file to start it?
Edit
In the other folder are also .py files like some FileImport.py or View.py. I separated these files that the whole project looks cleaner.
The Folder structure looks as follows:
-Views
---MainView
---UpdateView
---AnotherView
-Controllers
---MainViewController
---UpdateViewController
-Model
---MainModel
I found the solution. As it appears the pyinstaller can find all dependencies when you compile your script via pyinstaller myscript.py. PyInstaller creates three different folders where all necessary files are located. In the dist folder one executable is located which can be used from computers without python installed.
I hope this helps somebody who has the same problem.

Clion is not recognizing .py files as python files when I added a CMakeLists for one cc file

CLion was working fine with my python files. Then I added one .cc and created a CMakeLists.txt for it, now when I tab to a python file all the imports and built-in functions like open or int are counted as errors.
I've looked into Settings -> Editor -> File Types and everything looks okay.
How can I fix this?
The problem was the cmakelists was in a subdirectory, when I moved it to the root folder the .py files are treated as expected

Where is my _pycache_ folder and .pyc byte code files?

I'm running Python 3.4.1 on Windows 7 and thought that after running my .py script in the command line, a directory named _pycache_ would be created in the same directory that my script ran in. It is not there, even after I made sure that 'Show hidden files, folders, and drives' was checked. I looked around here and on Google but can't seem to get an answer that makes this visible.
Can someone help? I'm new to Python and would like to look over the byte code files.
The directory is called __pycache__ (with double underscores).
It'll only be created if Python has permission to create a directory in the same location the .py file lives. The folder is not hidden in any way, if it is not there, then Python did not create it.
Note that .pyc bytecode cache files are only created for modules your code imports; it is not created for the main script file. If you run python.exe foobar.py, no __pycache__/foobar.cpython-34.pyc file is created.

Python files created do not save as python file

https://drive.google.com/file/d/0BwR0ium7uufRWnhXWWNFcVJ3S2c/edit?usp=sharing
The files that are blank and say "Credit Card Balance" and "mine" are the files which I created by opening up a new document in IDLE and creating a file. The ones with the little pictures and say ps2_newton are programs which I downloaded off of the internet. I can't open my files which I created like they are regular programs and have to open them under notepad to see the code. Why is this? How can I change my files to look and act like the ones I downloaded?
The files you downloaded have an extension of ".py", which tells Windows to open them with the Python launcher, while the ones you created have no extension, or an extension of ".txt", which tells Windows to open them in Notepad.
Since ".py" and ".txt" are both "known extensions", and you have Explorer configured to hide known extensions (which I believe is the default in all versions of Windows), it doesn't show them to you. However, you can see the difference in the icons.
You can configure Explorer to show all extensions, but this site is not the place to ask about that—try SuperUser.
Meanwhile, when you save your scripts, make sure you save them with a .py extension (or, if they're Tkinter or other GUI apps, with a .pyw extension).
I had the same problem. But I just deleted all of the é's and á's etc. And it worked! So try that IF it is an .py file. Otherwise I understand if it does not work becouse it isn't a python file. You change that by doing this: Save as. Just simply type .py after the file name.
PS Sorry for my bad english...

Categories

Resources