Cannot import module - why does sys.path not find PYTHONPATH? - python

I am running Python 3.6.1 on a Windows 7 machine. I have some scripts in H:\Myname\Python\myscripts.
I have created the user variable PYTHONPATH and set it to the folder above. I do not have admin rights so I can create a user variable only.
In that folder, I have a myscripts.py file with some functions.
If I try to access it running import myscripts from a file stored elsewhere, it doesn't work: I get a ModuleNotFoundError
If I print sys.path, the folder I have set in PYTHONPATH is not there.
Why? What am I doing wrong? Isn't sys.path supposed to show PYTHONPATH?
Does the fact that H is a network drive have anything to do with it?
I can't seem to find anything on the web for this problem in relation to Windows (but lots for Unix systems).

A common way to fix this quickly is to use
sys.path.append("path/to/module")
Be careful with '\\' if you are using Windows.
Not exactly answering your question but this could fix the problem.

It is likely that there is a ._pth file overriding the default behavior of sys.path. Remove or rename the python._pth (or python36._pth) file.
There was a python37._pth file included in my download of the Windows embedded zip file that caused sys.path to not include PYTHONPATH.
To completely override sys.path, create a ._pth file with the same
name as the DLL (python37._pth) or the executable (python._pth) and
specify one line for each path to add to sys.path. The file based on
the DLL name overrides the one based on the executable, which allows
paths to be restricted for any program loading the runtime if desired.
When the file exists, all registry and environment variables are
ignored, isolated mode is enabled, and site is not imported unless one
line in the file specifies import site.
Using Python on Windows - Finding Modules

Related

How to use py file as external module without init

I am looking for a solution that will allow me to use py file as a little library.
Short description:
I have a py script and there are a lot of common functions in it.
Instead of each time use a big file I want to throw all common functions into separate py file, put into folder Tools and use it when I need.
My problems is that I cannot import this file from Tools, because my script does not see it.
My folder structure:
C:\some\folders\here\my\folder\script.py
C:\some\folders\here\Tools\Library\library.py
Also, it is not good for me to user init.py, because I haven't any python project, it is just one file without any other things.
Are there any normal solutions?
Python interpreter searches for modules in 3 places:
current directory, that means the directory from which you run the script with your import statement
list of directories from PYTHONPATH
installation-dependent list of directories, this is configures when Python in installed
You can also modify sys.path at runtime and include a directory where your module is located, but this is the worst solution and it's usually discouraged to do so.
Setting PYTHONPATH will most likely be a solution in your situation.

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.

How to import modules from alternate locations when using Python IDLE?

I've been trying to figure this out for more than 2 days, screening the internet and the tutorial, but yet I don't have solved my problem. I'm a real newb and don't yet really know what I'm doing..
Software I use:
Mac OS X 10.6
Python v3.2.2
Interactive interpreter (IDLE)
Problem:
IDLE's default directory is /Users/ME/Documents/. Files with the extention .py can only be opened when located in this directory. However, I made a folder where I would like to save all the .py files etc that have to do with this software. Currently, IDLE cannot load .py files from the chosen directory by me.
What I did first was I added to IDLE:
import sys.
sys.path.append('Users/Mydir/')
sys.path
However, in an already existing thread from 2010 I read sys.path is for the Interpreter ONLY, and that if I am to change this I need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
However, I'm confused how to use this and cannot find answers to my following questions:
1) PYTHONPATH (.py?) is already existing on my computer when I installed the program?
If YES, where is it? I cannot find it anywhere.
If NO, I need to create one. But where and what should be the content so that IDLE can load files from a non-default directory? Should it contain only the words in bold?
I hope I made my problem clear.
Cheers
It's not totally clear to me what you mean by load. That could mean Open and Close files in the IDLE editor. Or it could mean being able to use the Python import statement to load existing Python modules from other files. I'll assume the latter, that by load you mean import.
There are two general ways to launch IDLE on Mac OS X. One is from the command line of a terminal session; if you installed Python 3.2 using the python.org installers, by default typing /usr/local/bin/idle3.2 will work. The other way is by launching IDLE.app from /Applications/Python 3.2, i.e. by double-clicking its icon. Because you say the default directory for files is your Documents folder, I'm assuming you are using the second method because IDLE.app sets Documents as its current working directory, which becomes the default directory for *Open*s and *Save*s and is automatically added as the first directory on Python's sys.path, the list of directories that Python uses to search for modules when importing.
If you want to add other directories to sys.path, as you've noted you can use the PYTHONPATH environment variable to do so. The standard way to do this is to add an export PYTHONPATH=... definition to a shell startup script, like .bash_profile. However, if you use IDLE.app, no shell is involved so commands in .bash_profile have no effect.
While there are ways to modify the environment variables for OS X GUI apps, in this case, a simpler solution is to use the other method to invoke IDLE, from the command line of a shell session, using either /usr/local/bin/idle3.2 or, if you've run the Update Shell Profile command in the /Applications/Python 3.2 folder (and opened a new terminal session), just idle3. Then, a PYTHONPATH environment variable you set up will be inherited by that IDLE.
BTW, there is no direct way to modify the initial current working directory of IDLE.app from Documents other than modifying the code in IDLE. If you start IDLE from a command
line, it inherits the current working directory of the shell.
[UPDATE] But rather than fooling around with defining PYTHONPATH, here is another even simpler, and probably better, approach that should work with either IDLE.app or the command line idle. It takes advantage of Python path configuration (.pth) files and user site-package directories. Assuming you are using a standard Python framework build of 3.2 (like from a python.org installer) on Mac OS X, create a path file for the directory you want to permanently add to sys.path. In a terminal session:
mkdir -p ~/Library/Python/3.2/lib/python/site-packages
cd ~/Library/Python/3.2/lib/python/site-packages
cat >my_paths.pth <<EOF
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2
EOF
Now, whenever you run that Python 3.2 or IDLE under your user name, the directories you have added to the .pth file will automatically be added to sys.path.
BTW, the exact path location of the user site-packages directory for versions of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other Unix-y systems, the default location for the user site-package directory is ~/.local/lib/python3.2/site-packages.
PYTHONPATH is an environment variable (see here and here). I don't have a Mac, but from the threads I have linked to you would type something like
launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH
on the command line to allow you to run Python scripts from /Me/Documents/mydir. Alternatively, put this line in a file called .bashrc in your home directory (~) and this path will be set each time each time you open a terminal. See here for a short introduction to .bashrc and other .bash* files. Hope that helps.
EDIT See also this question.

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