Everything works as intended on my machine :)
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/sergey')
>>> sys.path
['', ..., '/home/sergey']
>>> sys.path.remove('/home/sergey')
>>> sys.path
['', ...]
>>>
What exactly have you tried?
Regarding your understanding of things - I'm afraid there are some mis-understandings:
sys.pathis a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something likeimport blahin your script, Python interpreter checks those directories one by one to check if there is a file calledblah.py(or a subdirectory namedblahwith__init__.pyfile inside)Current directory is where the script is located, not where Python interpreter is. So if you have
foo.pyandbar.pyin a directory, you can useimport barinfoo.pyand the module will be found because it's in the same directory.$PYTHONPATH is an environment variable which is getting appended to
sys.pathon interpreter startup. So, again, it is related to module search path and has nothing to do with starting Python from command line.Correct, you can modify
sys.pathat runtime - either when running a python script on in IDLE
See sys.path and site for more details.
Answer from Sergey on Stack OverflowEverything works as intended on my machine :)
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/sergey')
>>> sys.path
['', ..., '/home/sergey']
>>> sys.path.remove('/home/sergey')
>>> sys.path
['', ...]
>>>
What exactly have you tried?
Regarding your understanding of things - I'm afraid there are some mis-understandings:
sys.pathis a list of directories which contain Python modules, not system libraries. So, simplifying, when you have something likeimport blahin your script, Python interpreter checks those directories one by one to check if there is a file calledblah.py(or a subdirectory namedblahwith__init__.pyfile inside)Current directory is where the script is located, not where Python interpreter is. So if you have
foo.pyandbar.pyin a directory, you can useimport barinfoo.pyand the module will be found because it's in the same directory.$PYTHONPATH is an environment variable which is getting appended to
sys.pathon interpreter startup. So, again, it is related to module search path and has nothing to do with starting Python from command line.Correct, you can modify
sys.pathat runtime - either when running a python script on in IDLE
See sys.path and site for more details.
We can try below to insert, append or remove from sys.path
>>> import sys
>>>
>>> sys.path.insert(1, '/home/log')
>>> sys.path.append('/home/log')
>>> sys.path
['', '/home/log']
>>> sys.path.remove('/home/log')
>>> sys.path
>>> ['']
>>>
python - How to permanently remove value from sys.path? - Stack Overflow
Unintended removal of valid "sys.path" entries by isolation script
how to clean sys.path - Post.Byes - Bytes
python - Remove entry from sys.path - Stack Overflow
If you simply delete the line "export PYTHONPATH=..." in .bashrc and do "source .bashrc", those directories would still be in sys.path.
Unlike "export PATH" in .bashrc, it seems that when you export some directories into PYTHONPATH, they are dump into some file which python can always check.
So, what you need to do is "export PYTHONPATH=" (export empty string) and do "source .bashrc". This will clean up everything you have export into PYTHONPATH before in .bashrc.
First, from terminal grab everything in your path by using
env | grep PYTHONPATH
Then, export your path and manually remove anything you no longer need:
export PYTHONPATH=[this is where you paste the corrected paths, no square brackets needed]
If you restart your session and you haven't modified anything in .bashrc, you can simply close and reopen your session.
Hi, I have a few python versions installed on my Windows 10 system, alongside Anaconda. I'm trying to get python integration with Stata to work, and that seems to only work with the Anaconda installation of python, but there seems to be a piece of another distribution in the sys.path
>>> for path in sys.path: ... print(path) ... C:\Program Files\Stata16\ado\base\py C:\ProgramData\Anaconda3\python37.zip C:\ProgramData\Anaconda3\DLLs C:\ProgramData\Anaconda3\lib C:\Program Files\Stata16 **C:\Users\akashian\AppData\Roaming\Python\Python37\site-packages** C:\ProgramData\Anaconda3 C:\ProgramData\Anaconda3\lib\site-packages C:\ProgramData\Anaconda3\lib\site-packages\win32 C:\ProgramData\Anaconda3\lib\site-packages\win32\lib C:\ProgramData\Anaconda3\lib\site-packages\Pythonwin . C:\Program Files\Stata16\ C:\Program Files\Stata16\ado\base\ C:\Program Files\Stata16\ado\base\py C:\Program Files\Stata16\ado\site\ C:\Program Files\Stata16\ado\site\py c:\ado\plus\ c:\ado\plus\py C:\Users\akashian\ado\personal\ C:\Users\akashian\ado\personal\py c:\ado\
The bolded line breaks the importation of sklearn and other modules, I think because it's a 32 bit python version. How do I get rid of this, besides popping it off the sys.path list every time you run the python interpreter in Stata?
was following a tutorial on creating a Python script for deleting files that are older than a certain amount of days. I was able to successfully create the script and have it do what I want but now I need it to scan multiple paths not just the one. How would I go about doing that?
This is what I have:
import os
import datetime
import glob
import sys
path = r'PATHWAY'
logging_path = r'PATHWAY'
# Create log directory, wills skip if exists
if not os.path.isdir(logging_path):
os.mkdir(logging_path)
else:
print ("Directory already exists")
today = datetime.datetime.today() # Get current time
os.chdir(path) # Changing path to current path
# Create log file with timestamp
file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt','a')
for root, directories, files in os.walk(path, topdown=False):
for name in files:
# Check last modified time
t = os.stat(os.path.join(root, name))[8]
filetime = datetime.datetime.fromtimestamp(t) - today
# Is file older than 3 days?, If yes, delete.
if filetime.days <= -3:
print(os.path.join(root, name), filetime.days)
file.write(os.path.join(root, name)+' created '+str(-1*filetime.days)+' days ago\n')
os.remove(os.path.join(root, name))This is what I tried doing:
import os
import datetime
import glob
import sys
path = [r'PATHWAY', r'PATHWAY2']
logging_path = r'PATHWAY'
# Create log directory, wills skip if exists
if not os.path.isdir(logging_path):
os.mkdir(logging_path)
else:
print ("Directory already exists")
today = datetime.datetime.today() # Get current time
os.chdir(path) # Changing path to current path
# Create log file with timestamp
file=open(logging_path+datetime.datetime.today().strftime('%d-%m-%Y')+'.txt','a')
for root, directories, files in os.walk(path, topdown=False):
for name in files:
# Check last modified time
t = os.stat(os.path.join(root, name))[8]
filetime = datetime.datetime.fromtimestamp(t) - today
# Is file older than 3 days?, If yes, delete.
if filetime.days <= -3:
print(os.path.join(root, name), filetime.days)
file.write(os.path.join(root, name)+' created '+str(-1*filetime.days)+' days ago\n')
os.remove(os.path.join(root, name))After trying this though I get an error about chdir on line 15. I'm clearly missing something but I don't know enough about Python or programming to figure that out yet.