Issue
I'm using Python 3.6 with Spyder in an Anaconda installation. I am trying to learn to manipulate the path that Spyder knows. I went to PYTHONPATH manager, and added /Users/ericauld/Desktop
.
I have a file called Something.txt
on my desktop, and Spyder will recognize it if I say '/Users/ericauld/Desktop/Something.txt'
, but not if I write Something.txt
, even though the path is now supposed to include my desktop.
I tried restarting the kernel, but that didn't change anything.
I'm sure my error is quite elementary. Sorry if this question has already been answered, I haven't found a duplicate.
Solution
...tells Python where to find installed packages if they're not installed in the standard (system) places
However it looks like you want to tell Spyder where to look for files your scripts will be using. In this case I can suggest two solutions:
- Set your working directory to the directory where you code and related files should reside.
Organize project/files into directories like so
Then give your script analyze_something.py
access to the files it needs like so:
import os
#%% Setup paths
script_dir = os.path.abspath('')
project_dir = os.path.dirname(os.path.abspath(''))
data_dir = os.path.join(project_dir, 'data')
print(script_dir)
# >> C:\Users\path_to_your_project\Example Project Folder\code
print(project_dir)
# >> C:\Users\path_to_your_project\Example Project Folder
print(data_dir)
# >> C:\Users\path_to_your_project\Example Project Folder\data
The benefit of the latter option is that it's more organized and more portable.
Finally, checkout using environments with conda when you really need portability.
Answered By - zelusp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.