Jupyter Notebook/Pandas not reading excel file after moving ipynb - python

I'm working on automating some reporting for work which starts off with a pd.read_excel method. It worked fine and I moved on with the rest of my code. When I was done, I had a few ipynb files on my desktop and moved them into a folder called "Python". After doing so, I'm getting a "No such file or directory" error when rerunning the code. The file is in a folder called "Reporting". I can leave them on the desktop for now, but typically have everything organized in folders to avoid clutter. What do I need to do to get my code to continue read in the excel files?
RawData=pd.read_excel("Reporting/LS_Questions.xlsx",skiprows=2)

Try using an absolute path rather than a relative path.
Right now your path assumes that the file is within the same directory as the notebook.
you can either:
use a full path a.i - "C:\User{user_name}\Desktop\LS_Questions.xlsx"
Move the CSV/XL file to the directory of the notebook.

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.

How to get the path to the directory where the .exe file is located after Pyinstaller compiles python script

I have an app that accesses .txt files that contains user data from a subdirectory where the .py file is located. After Pyinstaller is used to compile the .py file to a .exe file the path changed to AppData\Local\Temp\_MEI185962\<subdirectory>\<user data.txt> when I print the path. My question is how do I come up with the path to where the .exe file is all the time, because I want to make my program be distributeable so I don't want to use a hardcoded path or if I move the folder around.
So for example if I have my program.exe in the directory C:/Users/<username>/Desktop/project then how do I access te subdirectory C:/Users/<username>/Desktop/project/data. I tried importing os.path and had
path.dirname(path.realpath(__file__))
to get the path, but it is still pointing to the C:\User\<username>\AppData\Local\Temp\_MEI185962\<subdirectory>\<user data.txt>.
I am new to compiling so I don't know much about pyinstaller at all. I also can't seem to find any thing on this issue either.

Python file always runs at C:/Users/User and not in the working directory

When I'm launching python file in VS Code that for example located at C:\Users\andre\Desktop\NewFolder\Something.py or D:\Pythons\Something.py
I'm getting C:\Users\andre via os.getcwd() and not the directory where python script is located.
VS Code takes the open folder as the workspace. So please open the whole folder in VS Code, not just a .py file, then VS Code will not be able to find your workspace and will use the computer user directory as the workspace by default. So naturally you get the wrong script path.
Example:
Open the folder as a workspace:
Just open a .py file:
VS Code open folde:

How do I load all of the .py files I have cloned from a Github repo in Colab?

I have cloned a GitHub repository and it has successfully imported all of the .py files I need on the left. I have set up a for loop that takes all of the .py files and saves them into an array. Now I want to loop through and run them all so that I can use their classes and functions without actually having to repaste their code into colab.
Example of list of .py files
I thought the below method would work:
Error message
But as shown, it says there is no such file or directory. Anybody have any ideas?

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.

Categories

Resources