Reputation: 647
When running an interactive session, PyCharm thinks of os.getcwd()
as my project's directory. However, when I run my script from the command line, PyCharm thinks of os.getcwd()
as the directory of the script.
Is there a good workaround for this? Here is what I tried and did not like:
going to Run/Edit Configurations and changing the working directory manually. I did not like this solution, because I will have to do it for every script that I run.
having one line in my code that "fixes" the path for the purposes of interactive sessions and commenting it out before running from command line. This works, but feels wrong.
Is there a way to do this or is it just the way it is supposed to be? Maybe I shouldn't be trying to run random scripts within my project?
Any insight would be greatly appreciated.
Clarification:
python path_to_myscript/my_script.py
(I actually press the Run button at PyCharm, but I think it's the same).Other facts that might prove worth mentioning:
os.path.join()
) from the current working directory (the project directory, ...\\project_name
) to the module's directory ...\\project_name\\Graphs
, where the file is located. However, when I run the whole script via the command line, the command reading the .txt file raises an Error, complaining that no file was found. By looking on the name of the file that was not found, I see that the full file name is something like this:
...\\project_name\\Graphs\\Graphs\\graph1.txt
It seems that this time the current working directory is ...\\project_name\\Graphs\\
, and my os.path.join()
command actually spoils it.Upvotes: 1
Views: 973
Reputation: 2488
I am purposely adding another answer to this post, in regards the following:
Other facts that might prove worth mentioning:
I have created a PyCharm project. This contains (among other things) the package Graphs, which contains the module Graph and some .txt files. When I do something within my Graph module (e.g. read a graph from a file), I like to test that things worked as expected. I do this by running a selection of lines (interactively). To read a .txt file, I have to go (using os.path.join()) from the current working directory (the project directory, ...\project_name) to the module's directory ...\project_name\Graphs, where the file is located. However, when I run the whole script via the command line, the command reading the .txt file raises an Error, complaining that no file was found. By looking on the name of the file that was not found, I see that the full file name is something like this: ...\project_name\Graphs\Graphs\graph1.txt It seems that this time the current working directory is ...\project_name\Graphs\, and my os.path.join() command actually spoils it.
I strongly believe that if a python script takes input from any file, that the author of the script needs to cater for this in the script.
What I mean is you as the author need to make sure you know the following regardless of how your script is executed:
These two you have no control over when you hand off your script to others, or run it on other peoples machines. The working directory is dependent on how the script is launched. It seems that you run on Windows, so here is an example:
C:\> c:\python\python your_script.py
The working directory is now C:\
if your_script.py
is in C:\
C:\some_dir\another_dir\> c:\python\python.exe c:\your_script_dir\your_script.py
The working directory is now C:\some_dir\another_dir
And the above example may even give different results if the SYSTEM PATH
variable is set to the path of the location of your_script.py
You need to ensure that your script works even if the user(s) of your script are placing this in various locations on their machines. Some people (and I don't know why) tend to put everything on the Desktop. You need to ensure your script can cope with this, including any spaces in the path name.
Furthermore, if your script is taking input from a file, the you as the author need to ensure that you can cope with changes in working directory, and changes of script directory. There are a few things you may consider:
C:\python_input\
ConfigParser
, you can search here on stackoverflow on many postsos.path.dirname(__file__)
) Ultimately this is all in your control, and you need to code to ensure it is working.
HTH, Edwin.
Upvotes: 1
Reputation: 2488
I user various methods in my python scripts.
set the working directory as first step of your code using
os.chdir(some_existing_path)
This would mean all your other paths should be referenced to this, as you hard set the path. You just need to make sure it works from any location and your specifically in your IDE. Obviously, another os.chdir()
would change the working directory and os.getcwd()
would return the new working directory
set the working directory to
__file__
by usingos.chdir(os.path.dirname(__file__))
This is actually what I use most, as it is quite reliable, and then I reference all further paths or file operations to this. Or you can simply refer to as os.path.dirname(__file__)
in your code without actually changing the working directory
get the working directory using
os.getcwd()
And reference all path and file operations to this, knowing it will change based on how the script is launched. Note: do NOT assume that this returns the location of your script, it returns the working directory of the shell !!
[EDIT based on new information]
By "interactive session" I mean being able to run each line individually in a Python/IPython Console
By running interactively line-by-line in a Python console, the __file__
is not defined, afterall: you are not executing a file. Hence you cannot use os.path.dirname(__file__)
you will have to use something like os.chdir(some_known_existing_dir)
to reference a path. As a programmer you need to be very aware of working directory and changes to this, your code should reflect that.
By "running from command line" I mean creating a script my_script.py and running python path_to_myscript/my_script.py (I actually press the Run button at PyCharm, but I think it's the same).
This, both executing a .py
from command line as well as running in your IDE, will populate the __file__
, hence you can use os.path.dirname(__file__)
HTH
Upvotes: 1