Atom does not open file over python code in the same directory

I tried to check to see if any similar issues were reported here but couldn’t find. So my apologies if this was addressed before.
Actually I think my problem involves cwd. I am using macOS for a while now. Before that I was using win 7. I usually run python codes on Atom with “atom-python-run” pack and it’s just smoothly runs everything on both win 7 and macOS. However, on macOS, when it comes to python codes that require opening a simple file (txt in this case), it gives a traceback:

FileNotFoundError: [Errno 2] No such file or directory: ‘filename.txt’

I made some research and found out that it might be with the cwd but couldn’t figure how exactly it affects the run of the python line when both files are in the same directory. Some people suggested importing pathlib with a line:
filepath = pathlib.Path(__file__).parent / fname when fname is: fname=input('Enter the file name: ')
I also found out that inputing the full path of the file to be opened is a solution too.
Both actually work well but when the assignments pile up, requiring me to work with many different file types in many different codes within a limited period of time, I think importing a lib when other learners aren’t or copying full path of the file each time, will mess things up.

On win7, the file I want to open via the python code on atom was as easy as to save the file in the same directory(folder) with the code file before f5/f6 it on Atom. On macOS, that does not work and I don’t know why.

I’ll share 2 screenshots;

  1. I directly put the file name as a string within open() method and getting the Errno 2:

  2. I input the file name on terminal window and still getting the Errno 2:

Atom is located on my desktop.
Python3 directory:

where python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3

Having left with no working solution I had also tried aliasing python to python3 on ~/.zshrc

where python
python: aliased to python3
/usr/bin/python

I appreciate for any help.

Regards.

I have no recent experience with Mac.
But this is a suggestion based on my own setup in Ubuntu:

install process-palette
apm install process-palette

Read the settings to create a custom process

If I go into Preferences > Settings > process palette
and click on settings … you scroll down to Examples

Add a command for running Python scripts

but the screenshot is difficult to follow

Continue down to Graphical editor

Your settings will typically be (taken from my configuration)

General Settings
Namespace: process-palette
Action Name: run-python
Shell Commands: python3 -u {fileAbsPath}
Working Directory: ~/anaconda3/bin/python
Keystroke: … left blank
Menu: Run,Python

Your settings might be different.

Note that the above configuration does not use hot keys (that Keystroke field I left blank) and instead I placed a Menu item: Run,Python

This has the effect of placing a Run button in Atom top bar named Run and under there you should see Python > Run Python.

All of my custom processes are listed here: Python, R, Lua, Java …

Now go to Atom top bar > Packages > Process Palette > Toggle

In the panel a list of custom processes is shown.

to the left of the name Process Palette: Run Python
there are two buttons:
Run Process
Edit

Click on the Edit configuration button.

There is a learning curve but when you use process-palette you will see that it can be your gateway to external processes such as running Python, running other tools in a toolchain.

Thanks for the reply. I followed your path step by step. Managed to get to add new item (Run) to the menu bar as you suggested. And ran, but this time I got the “permission denied” error.
And the atom-python-run’s f5 still gives the Errno 2 traceback.

Your custom process seems to be working.
My thought is that you need to examine the permissions
of your file you are accessing. I don’t know enough about Mac but in Ubuntu I would examine properties of folder/files and check that Atom has the same. You might have created your file in an outside editor with different ownership for example.

[P.S.] You write "atom-python-run’s f5 " … but F5 is no longer in the workflow. You are still trying the old workflow. Get rid of it, get rid of atom-python-run, and stick with only process-palette. Do you see any error when you click on Run Python in top bar (not F5)?

1 Like

I now changed my shell command to your exact one (don’t know if -u works for me but I put the line as it is)
And changed the working directory to my python root folder as well.
I ran it again, this time no yellow box with permission denied error popped. It just asks the file name (as the input of the code line) but the process palette toggle doesn’t allow me to enter the file name. It’s kind of read-only.
In the mean time, I tried atom-python-run again, and still getting the same traceback.

It is either one or other … not both as I wrote above. Delete atom-python-run package and focus on the more generic process-palette. And I do not understand why you have characters after the menu iten, Run Python. I don’t see those. Check settings again.

Further note:

You do not type in filename. Instead look to the variables button to the right of the field (also read all the available variables) and choose the appropriate variable or mix of variables to place in the field. You can concatenate variables.

1 Like

But it still runs all other codes other than the ones requiring to open a file. And it also got f6 the same.

Will do.

I think you’re talking about me having toggled the Run item to show you that it’s there? Run menu item is there all by itself, I like it very much :slight_smile:

edit: I think you referred the keyboard characters. I addet ctrl-alt-r short cut. That’s them I guess.

No sir.

See edits I added on entering Atom editor variables as we crossed posts.

Also another tip …

I have another custom process, Python Version, again under Run menu dropdown list, which has this in command field:

which python && python --version   && echo $PATH

Thanks.
I do not see variables button here. If you could just send a screenshot that’d help.

Will you please explain this a bit? I ran this in terminal and got something like this:

python: aliased to python3

Python 3.9.1

/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/bin/Python:/usr/local/bin/python3

Should I just go ahead and edit the Run menu and add that line in a command field? The line starts with “which python”

Here is a screenshot of part of the process-palette toggled panel.

See the Insert variables to the right of the command field. You will see a list of variables to choose from.

The Python Version custom process is not mandatory just useful. You can add any custom process item you think might be useful for development. Here I have added a third custom process for running R. Build your own library of custom processes and they can be added to your top menu bar. I do not use keys since there is always the risk of conflicting with other package hot keys…

1 Like

Thanks again.

How can I enter the input file name if I were to run a code that asks me to input a file name?

I think this is going off topic and getting into python programming methods.

Here is one link.

Thank you D.
I disagree. I think it’s actually, literally the topic.
As I mentioned in the thread post above, the main problem is I can’t open files running codes on Atom despite them files being at the exact same folder with the .py file the code is in, which means same directory.
I also added some people had suggested me try importing libpath but that would not be a good solution as the increased workload in coming weeks will make writing addition lines within codes would take time and require building an unnecessary routine. So that’s basically what I asked help for.

But I appreciate your help anyways sir.

O.K. I’m out for today.

Let me explain my reasoning.
I read your post #1 with screenshots and it underlined one symptom
FileNotFoundError
setting aside the other issues about cwd.

You wrote:
“I made some research and found out that it might be with the cwd but couldn’t figure how exactly it affects the run of the python line when both files are in the same directory.”

And
“I also found out that inputing the full path of the file to be opened is a solution too.”

Accordingly I suggested using process-palette which (through a rich set of Atom variables) does allow you to “input the full path”.

One issue in your workflow was solved.

But I see that you raise another issue: not being able to open a file mbox.txt in the same folder as the running only_test.py

Please now refer to this search (search field at top of this page).

https://discuss.atom.io/search?q=python%20input

There are many new users asking the same question.

More reading of above threads is required. I leave that to you.

But … I will offer one tip. You can get such issues to work by passing a command string to an external shell. In Ubuntu this is gnome-terminal or yakuake. You can do likewise in Mac. In other words Atom is part of a toolchain. I have yet another process defined in process-palette which serves this purpose.

Here it is for the record (fasten seatbelt):

In process-palette I define a new custom process Run Proxy.
I arrange this to have an entry in top menu bar . Proxy.
I define command as follows:

cd ~/.atom-scripts/ && python3 Atom-argparse.py  {clipboard}  {fullCommand}  {fileExt} {fileName}  {fileNameExt} {filePath} {fileDirPath} {fileDirAbsPath} {fileProjectPath}

This leverages Atom variables (found in process-palette variables) and passes the entire lot of variables to an external python script
Atom-argparse.py

This script inputs and parses arguments and from thereon Atom can communicate with the normal desktop environment including running the python script seen in Atom editor in an external tool or shell.

As one example (in Mac) you can set up a communication between Atom and Alfred.

In Ubuntu this might be Atom-Albert.

The reverse flow applies also: Albert-Atom, Alfred-Atom, Krusader-Atom.