Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm - how do I set a consistent working directory when running current file?

Tags:

python

pycharm

When running the current file (via PyCharm's "run current file" action), it sets the working directory to the /Users/xxx/Documents/myproject/xy folder (the file path is /Users/xxx/Documents/myproject/xy/myfile.py).

However, I want the working directory to be the /Users/xxx/Documents/myproject/folder. I don't understand why it automatically uses the directory the python file is in and not what I actually specified as working directory.

The main problem comes from this line of code in that python file:

df = pandas.read_csv("output/file.csv")

There is a /Users/xxx/Documents/myproject/output/file.csv

But it fails to open it, since it looks inside the "xy" folder and not the "myproject" folder for "output/file.csv". I could of course do something like pandas.read_csv("../output/file.csv")

but then that would break every time I refactor my code and move that file into a different folder. Is it not possible to have a working directory in pycharm which is consistent, no matter what file I run in my project?

Also: for some reason, this import: from lib.mylibrary import somefunction works, even though that file is saved in /Users/xxx/Documents/myproject/lib/mylibrary.py

So for imports, it does seem to search from the working directory. However, that does not happen for any other stuff. Why? Do imports have different working directories than other code?

like image 401
Ookami Avatar asked Nov 01 '25 07:11

Ookami


2 Answers

I find that relying on implicit working directory is bad practice in scripts, unless it's specifically intended to run with a variety of working directories.

Instead I set the paths relative to the script itself:

from pathlib import Path

base_dir = Path(__file__).parent

df = pandas.read_csv(base_dir / "output/file.csv")

This is much more consistent, no matter where you run the file from.

Another option is to simply make the filepath a CLI argument of your script, and read it in with something like docopt, so that you never have to deal with working directories.

Yet one more possibility is to read from STDIN (sys.stdin) instead of from a file:

cat output/file.csv | python my_script.py

I think pandas supports this, but it only works if you'll be reading only one file.

So for imports, it does seem to search from the working directory. However, that does not happen for any other stuff. Why? Do imports have different working directories than other code?

Imports are different from working directory. When you run scripts from a shell, you might have something like (assuming you configured your shell to show the current working directory):

/home/ookami/Documents $> python ~/code/my_script.py

In this case, /home/ookami/Documents is your current directory, aka the working directory, aka the PWD environment variable (managed by your shell). ~/code/my_script.py aka /home/ookami/code/my_script.py is the absolute path to your script. If your script tries to read from output/file.csv that will actually be $PWD/output/file.csv aka /home/ookami/Documents/output/file.csv.

This is of course an ancient Unix mechanism for allowing you to have multiple output/file.csv in different locations, and letting you determine which one will be read based on which directory your shell is in. When you use a GUI abstraction like Pycharm on top of the shell, it begins to lose meaning and results in confusion. I would recommend you not use the Pycharm "run" feature, and get comfortable running Python scripts from the command line, it will save you much trouble in the long run. While you're at it, read up on the "Unix philosophy", it will enlighten you and teach you to write much more elegant programs.

Imports, on the other hand, do not depend on where you are running the script from. They are normally either relative to the script itself (__file__) or absolute paths (your system libraries). They come from PYTHONPATH which is explained in Python documentation.

like image 160
Dommondke Avatar answered Nov 02 '25 22:11

Dommondke


When creating a new project PyCharm creates a virtual environment. The pip installs are all installed only into this local project folder and working folder is set to the root project folder.

If you open folder without creating project I can imagine defaulting to python's default location and the root folder can be set to the incorrect location.

  1. I recommend using the virtual environment (set from settings -> project -> project interpreter)
  2. You can set the working dir, arguments and executed python script in the right top corner of PyCharm -> usually with name of a python file -> then edit configuration
like image 35
Rozto_ Avatar answered Nov 02 '25 23:11

Rozto_