Python Forum
setting working directory in pycharm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setting working directory in pycharm
#1
parent/
    one/
        __init__.py
        sample1.py
    two/
        __init__.py
        sample.py
When i run the code through command prompt like <c:/users/parent> python two\sample.py,print(os.getcwd())gives "c:/users/parent".But when i run sample.py through Pycharm, print(os.getcwd())gives "c:/users/parent/two".Is there a way to set working directory in Pycharm so that in all instances it takes working directory as "c:/users/parent"
Reply
#2
when running in IDE,
os.chdir(os.path.abspath(os.path.dirname(__file__)))
This assures that the starting directory for relative operations is always the directory where python script resides.
Reply
#3
(Dec-06-2018, 10:08 AM)Larz60+ Wrote: when running in IDE,
os.chdir(os.path.abspath(os.path.dirname(__file__)))
This assures that the starting directory for relative operations is always the directory where python script resides.

But this statements returns None.
Is there any way to change the working directory in Pycharm itself,instead of code level,as I need the code to work through both cmd and pycharm
Reply
#4
I think you are mixing stuff together,what you do in first post is make a package.
(Dec-06-2018, 11:54 AM)saisankalpj Wrote: Is there any way to change the working directory in Pycharm itself,instead of code level,as I need the code to work through both cmd and pycharm
To make a folder that Python find wherever you in PyCharm or command line,
add that folder to sys.path(this is where Python search for .py files).

I find it easiest to not mess with PYTHONPATH for adding own folders permanently.
The site module offers a method that takes care of adding to sys.path without duplicates and .pth files.
Make a sitecustomize.py file in C:\Python37\Lib\site-packages or your site-packages folder.
# sitecustomize.py
import site

site.addsitedir(r'E:\div_code')
Test that it work.
C:\
λ ptpython
>>> import sys
>>> from pprint import pprint

>>> pprint(sys.path)
['C:\\python37\\Scripts\\ptpython.exe',
 'c:\\python37\\python37.zip',
 'c:\\python37\\DLLs',
 'c:\\python37\\lib',
 'c:\\python37',
 'c:\\python37\\lib\\site-packages',
 'E:\\div_code'] <----
For OS and Path look at Python 3.6/3.7 and pip installation under Windows

Now if a package(your first code) is placed eg in E:\div_code,then python will find it.
Reply
#5
(Dec-06-2018, 01:10 PM)snippsat Wrote: I think you are mixing stuff together,what you do in first post is make a package.
(Dec-06-2018, 11:54 AM)saisankalpj Wrote: Is there any way to change the working directory in Pycharm itself,instead of code level,as I need the code to work through both cmd and pycharm
To make a folder that Python find wherever you in PyCharm or command line,
add that folder to sys.path(this is where Python search for .py files).

I find it easiest to not mess with PYTHONPATH for adding own folders permanently.
The site module offers a method that takes care of adding to sys.path without duplicates and .pth files.
Make a sitecustomize.py file in C:\Python37\Lib\site-packages or your site-packages folder.
# sitecustomize.py
import site

site.addsitedir(r'E:\div_code')
Test that it work.
C:\
λ ptpython
>>> import sys
>>> from pprint import pprint

>>> pprint(sys.path)
['C:\\python37\\Scripts\\ptpython.exe',
 'c:\\python37\\python37.zip',
 'c:\\python37\\DLLs',
 'c:\\python37\\lib',
 'c:\\python37',
 'c:\\python37\\lib\\site-packages',
 'E:\\div_code'] <----
For OS and Path look at Python 3.6/3.7 and pip installation under Windows

Now if a package(your first code) is placed eg in E:\div_code,then python will find it.
I have added Pythonpath variable as "c:/users/parent",bcos of which code works fine in cmd .Here the current working directory is "c:/users/parent" ,as i run code through the command <c:/users/parent> python two\sample.py.
But when i run sample.py through Pycharm,by hitting on run button, print(os.getcwd())gives "c:/users/parent/two".
I want the os.getcwd() as c:/users/parent,because Pycharm goes inside two/ to search for a specific file while the file is under one/
Reply
#6
(Dec-06-2018, 10:08 AM)Larz60+ Wrote: when running in IDE,
os.chdir(os.path.abspath(os.path.dirname(__file__)))
This assures that the starting directory for relative operations is always the directory where python script resides.

I have 10 .py files each under both directories.do i need to keep this statement in all .py files?
Reply
#7
The only thing this statement does is assure that the starting directory is always set as where python script is located.
Quote:do i need to keep this statement in all .py files
That's a qualified no, with qualification being that order of execution may cause path changes.
Safe answer is yes.
If you follow snippsat's advice, you shouldn't need it at all.
I always include this statement, it's only executed once, and I always know what my current working directory is.
Reply
#8
(Dec-06-2018, 01:10 PM)snippsat Wrote: I think you are mixing stuff together,what you do in first post is make a package.
(Dec-06-2018, 11:54 AM)saisankalpj Wrote: Is there any way to change the working directory in Pycharm itself,instead of code level,as I need the code to work through both cmd and pycharm
To make a folder that Python find wherever you in PyCharm or command line,
add that folder to sys.path(this is where Python search for .py files).

I find it easiest to not mess with PYTHONPATH for adding own folders permanently.
The site module offers a method that takes care of adding to sys.path without duplicates and .pth files.
Make a sitecustomize.py file in C:\Python37\Lib\site-packages or your site-packages folder.
# sitecustomize.py
import site

site.addsitedir(r'E:\div_code')
Test that it work.
C:\
λ ptpython
>>> import sys
>>> from pprint import pprint

>>> pprint(sys.path)
['C:\\python37\\Scripts\\ptpython.exe',
 'c:\\python37\\python37.zip',
 'c:\\python37\\DLLs',
 'c:\\python37\\lib',
 'c:\\python37',
 'c:\\python37\\lib\\site-packages',
 'E:\\div_code'] <----
For OS and Path look at Python 3.6/3.7 and pip installation under Windows

Now if a package(your first code) is placed eg in E:\div_code,then python will find it.

so if i create sitecustomize.py file and keep it in C:\Python37\Lib\site-packages,will even
pycharm recognize this as current working directory or it is only for command prompt?
Reply
#9
(Dec-06-2018, 03:47 PM)saisankalpj Wrote: so if i create sitecustomize.py file and keep it in C:\Python37\Lib\site-packages,will even
pycharm recognize this as current working directory or it is only for command prompt?
Yes,but you have you make sure in Configuring Python Interpreter that 3.7 is selected.

A package should be stand alone,and the use import statement to access files in it.
So the only requirement is that package folder is in sys.path,then will Python(3.7 in this case) find it.
Example:
my_pack\
|-- __init__.py 
  one\
  |-- __init__.py  
  |-- sample_1.py     
  two\
  |-- __init__.py 
  |-- sample_2.py
__init__.py: the one under my_pack folder,other are empty.
from .one.sample_1 import power_of
from .two.sample_2 import upper_name 
sample_1.py:
def power_of(arg):
   return arg ** arg 
sample_2.py:
def upper_name(name):
    return name.upper()
Now can i test my package:
>>> import my_pack

>>> my_pack.power_of(5)
3125

>>> my_pack.upper_name('tom')
'TOM' 

# Or can import like this
>>> from my_pack import power_of

>>> power_of(22)
341427877364219557396646723584
As you see i using the main __init__.py to lift sub modules up from folders.
This make it easier for users my_pack.power_of(5) insted of my_pack.sample_1.power_of(5).
This is what i what i always do make import easier if other shall use my package.

So this package work stand alone,for Python to find it need to be in sys.path,so in my cases E:\div_code\my_pack
Then it work from Editors or command line.
Reply
#10
(Dec-06-2018, 06:54 PM)snippsat Wrote:
(Dec-06-2018, 03:47 PM)saisankalpj Wrote: so if i create sitecustomize.py file and keep it in C:\Python37\Lib\site-packages,will even
pycharm recognize this as current working directory or it is only for command prompt?
Yes,but you have you make sure in Configuring Python Interpreter that 3.7 is selected.

A package should be stand alone,and the use import statement to access files in it.
So the only requirement is that package folder is in sys.path,then will Python(3.7 in this case) find it.
Example:
my_pack\
|-- __init__.py 
  one\
  |-- __init__.py  
  |-- sample_1.py     
  two\
  |-- __init__.py 
  |-- sample_2.py
__init__.py: the one under my_pack folder,other are empty.
from .one.sample_1 import power_of
from .two.sample_2 import upper_name 
sample_1.py:
def power_of(arg):
   return arg ** arg 
sample_2.py:
def upper_name(name):
    return name.upper()
Now can i test my package:
>>> import my_pack

>>> my_pack.power_of(5)
3125

>>> my_pack.upper_name('tom')
'TOM' 

# Or can import like this
>>> from my_pack import power_of

>>> power_of(22)
341427877364219557396646723584
As you see i using the main __init__.py to lift sub modules up from folders.
This make it easier for users my_pack.power_of(5) insted of my_pack.sample_1.power_of(5).
This is what i what i always do make import easier if other shall use my package.

So this package work stand alone,for Python to find it need to be in sys.path,so in my cases E:\div_code\my_pack
Then it work from Editors or command line.

Hi,
I have included my package in sys.path(For ex:E:\div_code\my_pack,in this case),and have linked it to python 37,But while running through Pycharm i am getting output of os.getcwd()as E:\div_code\my_pack\two,when i run sample_2.py.I want the output to be E:\div_code\my_pack
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 740 Nov-28-2023, 11:36 PM
Last Post: chitarup
  Help setting up Pycharm & Remote Pi PickyBiker 3 1,961 Sep-17-2021, 04:04 PM
Last Post: PickyBiker
  Setup working directory in IDLE Pavel_47 3 3,794 Mar-06-2021, 08:59 AM
Last Post: Pavel_47
  [PyCharm] Working with virtual environment AndrzejB 1 2,133 Jul-18-2020, 06:22 PM
Last Post: AndrzejB
  CWD (Current working directory) pcsailor 5 4,689 Nov-11-2018, 10:14 AM
Last Post: Larz60+
  Configuring interpretor in PyCharm: [Errno 2] No such file or directory BoaCoder3 0 8,057 Sep-15-2018, 01:21 PM
Last Post: BoaCoder3
  how to save python file to working directory rpdohm 3 6,030 Sep-14-2017, 06:16 PM
Last Post: rpdohm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020