Why I need to specify working directories and path? - python

Whenever I do a project for computer science, I have to make sure all of my files are located in the same folder, or I'll have errors. If I want to use a file from somewhere else, I have to insert it into the path. I do these things but don't fully understand what is happening or why. Why is the path changed in the runtime environment?

When you run a python script you are executing it in the current working directory /home/user/python.py for example. That means this script since it lives in /home/user has access to everything in that path. However you should be able to access any other directory from here as long as the permissions are setup right. You would do that by using relative paths. so for example /home/user/python.py could access a file that is /home/example/file.txt by giving it the path ../example/file.txt from the python project.

Have you tried adding the path using sys.path.append? If you don't want to do that every time then you can set (Windows) %PYTHONPATH% to include your custom path. That's what I do for my include folder.

Related

Why this Relative Path of a file is not working (..\) and (.\)

I have been working on a project but while opening files from different folders I came across this peculiar problem where the relative path for a file within the same directory is not opening with this statement:-
But works perfectly fime with this statement:-
even when I don't mention folder description, like directly using the file name in statement, it shows error:-
Can anyone give a explaination for what is going wrong here?
Also this file is working in this particular location only, i.e. if I move the complete project to another location on the computer or to another computer, all the methods fail and I need to mention absolute path in order to make it work.

Changing Directory With Python, Directory Not Found Error

I am trying to os.chdir() into system32 from python on windows, but when i attempt to change into this directory I am getting this error:
FileNotFoundError: [WinError 2] The system cannot find the file specified:
'/System32/
So obviously Python can't see this directory but I don't know why because os.listdir() shows this directory in the list. Does this have to do with the permissions that python has? Ultimately my goal is to change into the winevt directory to pull and dump the log files and to check for any errors, so any way to grab these is completely fine. My intuition was simply to change into the directory, open and read the log files and then check for errors, then print and report those errors.
Your current working directory may be different from the one where folder is.
Use this to check your current working directory before changing the directory.
print('Present DIR is : ',os.getcwd())
Then go to the correct directory and change the directory.
When you try to get into System32, use absolute path rather than the relative path, with the following:
os.chdir(r'C:/Windows/System32')
or in your case:
os.chdir(r'C:\Windows\System32\winevt\Logs')
As Archit said, you might not be in the correct directory.
The solution to this problem was a little bit hard to come by. I first tried uninstalling python 32 bit but that just broke everything.
I eventually installed python36 and added the python36.dll and the location of this dll to the user and system path (on Windows). Then I made sure to remove anything in the path involving python 34 or python36-32 which is the 32 bit version of python. This then allowed my to easily os.chdir into system32

What is the purpose of the environmental variable PYTHONPATH

On windows 7, I currently don't have a python path. Can I safely make one? If so, how do I do it?
Upon making this variable, I can no longer load Spyder (IDE) without it crashing. Does anyone know why?
I would like to edit my existing python path if possible, but just don't know why it isn't already there in environmental variables.
I would ultimately like to be able to run "python myscript.py" and have myscript be in a different directory from the call directory.
PYTHONPATH adds new paths to the ones Python uses by default. The path in total determines where Python will look for modules when you import them.
Look at sys.path to see the combination of the defaults with your PYTHONPATH environment variable. It's likely that Spyder is loading a module that exists in two different places and the wrong one comes first.
When you import modules in python, python searches for the module in the directories in PYTHONPATH, in addition to some other directories.
In order to be able to run your script as > myscript.py, you want to put your script somewhere on PATH (here are some instructions for viewing or updating PATH), this is where the OS looks for scripts and programs when you give it a command. I believe that in windows the .py extension must be associated with python for windows to know that myscript.py should be run using python. This should happen automatically when python in installed, but maybe someone with more windows knowledge can comment on this.
it has role similar to path. this variable tells the python interpreter where to
locate the module files imported into a program. it should include the python source library directory and the directories contain in python source code

Setting python path

I have a Django app and I'm getting an error whenever I try to run my code:
Error: No module named django_openid
Let me step back a bit and tell you how I came about this:
I formatted my computer and completely re-installed everything -- including virtualenv, and all dependent packages (in addition to Django) required for my project based on settings in my requirements.txt folder
I tried doing python manage.py syncdb and got the error
I googled the issue, and many people say it could be a path problem.
I'm confused as to how I go about changing the path variables though, and what exactly they mean. I found some documentation, but being somewhat of a hack-ish noob, it kind of goes over my head.
So my questions are:
What exactly is their purpose -- and are they on a system based level based on the version of Python or are they project dependent?
How can I see what mine are set to currently?
How can I change them (ie. where is this .profile file they talk of and can I just use a text editor)
Any input you would have would be great as this one is stumping me and I just want to get back to writing code :-)
The path is just the locations in your filesystem in which python will search for the modules you are trying to import. For example, when you run import somemodule, Python will perform a search for somemodule in all the locations contained in the path (sys.path variable).
You should check the path attribute in sys module:
import sys
print sys.path
It is just a regular list, sou you could append/remove elements from it:
sys.path.append('/path/to/some/module/folder/')
If you want to change your path for every python session you start, you should create a file to be loaded at startup, doing so:
Create a PYTHONSTARTUP environment variable and setting it to your startup file. E.g.: PYTHONSTARTUP=/home/user/.pythonrc (in a unix shell);
Edit the startup file so it contains the commands you want to be auto-executed when python is loaded;
An example of a .pythonrc could be:
import sys
sys.path.append('/path/to/some/folder/')
Do you really need to alter the path? It's always best to actually think about your reasons first. If you're only going to be running a single application on the server or you just don't care about polluting the system packages directory with potentially unnecessary packages, then put everything in the main system site-packages or dist-packages directory. Otherwise, use virtualenv.
The system-level package directory is always on the path. Virtualenv will add its site-packages directory to the path when activated, and Django will add the project directory to the path when activated. There shouldn't be a need to add anything else to the path, and really it's something you should never really have to worry about in practice.

Noob Question: How to import some imodule in Python?

I am using Python 2.5 on CentOS 5.5
I have got a file called MultipartPostHandler.py, I am supposed to use it like this:
import MultipartPostHandler
But I dont know where should I put the file MultipartPostHandler.py so I can use it. Thanks a lot!
http://docs.python.org/tutorial/modules.html#the-module-search-path
When a module named spam is imported,
the interpreter searches for a file
named spam.py in the current
directory, and then in the list of
directories specified by the
environment variable PYTHONPATH. This
has the same syntax as the shell
variable PATH, that is, a list of
directory names. When PYTHONPATH is
not set, or when the file is not found
there, the search continues in an
installation-dependent default path;
on Unix, this is usually
.:/usr/local/lib/python.
Actually, modules are searched in the
list of directories given by the
variable sys.path which is initialized
from the directory containing the
input script (or the current
directory), PYTHONPATH and the
installation- dependent default. This
allows Python programs that know what
they’re doing to modify or replace the
module search path.
So, you have to put it into the current directory or use sys.path to show your program where the searched module is.
The easiest is to put it in the same folder as the code you're writing.
If you got it from somebody who provides an installer, then to be safe, just run the installer.
If it's just something you grabbed specifically for this project, put it in the same folder as the .py files that you're writing.
Otherwise (if you're planning to use it for a few projects and don't want to copy/paste the file), the safest place to put it is probably in the /lib/site-packages sub-directory of the directory where Python is installed.
The easiest - yes, is to put it in the same directory where you have the importer python code. Also, you can export PYTHONPATH environment variable which points to the directory (or directories separated by :) where you have MultipartPostHandler.py. Another option: make it distributable. This will be useful if you are going to publish your code.

Categories

Resources