This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:
import sys
sys.path.insert(0,'/path/to/mod_directory')
That said, there are usually better ways to manage imports than either using PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.
This is working as documented. Any paths specified in PYTHONPATH are documented as normally coming after the working directory but before the standard interpreter-supplied paths. sys.path.append() appends to the existing path. See here and here. If you want a particular directory to come first, simply insert it at the head of sys.path:
import sys
sys.path.insert(0,'/path/to/mod_directory')
That said, there are usually better ways to manage imports than either using PYTHONPATH or manipulating sys.path directly. See, for example, the answers to this question.
You could use:
import os
path = 'the path you want'
os.environ['PATH'] += os.pathsep + path
If you're using bash (on a Mac or GNU/Linux distro), add this to your ~/.bashrc
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.
superuser.com may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.
How do I add a directory to PATH? I am having issues running a python script by just its name.
How to add my project folder to the PYTHONPATH?
how to add a directory to sys.path?
How to add python to path and how to use it in commandline/powershell?
I am trying to run a python script by just its name (practice.py) if possible. Or just python practice.py. Either way I would like to be able to run it regardless of what directory I am in. This file is in my Scripts directory. My path is /home/user/miniconda3/bin:/home/user/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:SCRIPTS_DIR. I added SCRIPTS_DIR. SCRIPTS_DIR = /home/user/Programming/Scripts. I added the path to my .bashrc. The file is executable. The script works if I am in the directory but I cannot figure out how to be able to call it from anywhere. I have tried multiple times to understand adding paths but so far no luck. Any help is appreciated.
I am using a scheduler app that requires the full path of my project.pythonfile.method to run the method that needs to be scheduled.
But it is not working.
I noticed even from project.pythonfile import method is not found. I never needed that as I am using relative paths until now (from .pythonfile import method). But as my scheduler is a third party app, I don't think it will work.
I did some research and I think I need to append my project folder to the PYTHON PATH variable. Does anyone know how to do that? is there a GUI in the Windows interface? or is it all command based?
I assume it is different from the OS Environment PATH variable? or no?
I am using Windows btw.
Hello everyone,
I have some modules I want to reuse, I don't want to parent import because they don't are related to the current project. The option I'll like to use is to add a directory to sys.path so they can be found. Also, I'd like to to achieve this preferably by configuration, not programmatically manipulating the sys.path value.
Just like venv does when activating the environment. I have looked into .venv/script/activate but there is no explicit instruction to include .venv/Lib/site-packages so, how does it do it?
How python finds its modules
Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).
By default, Python looks for its modules and packages in $PYTHONPATH.
To find out what is included in $PYTHONPATH, run the following code in python (3):
import sys
print(sys.path)
How to add a directory
Occasionally
From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
For example:
if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:
import sys
sys.path.insert(0, "/home/myname/pythonfiles")
And I can simply import the file module_1.py by:
import module_1
When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py
For example:
To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `
from <packagename> import module_2
Adding directories to $PYTHONPATH permanently:
Add the following line to your ~/.profile file.
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Subdirectories
From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:
import packagename.subfolder.module_3
Given the fact that all subfolders in the package include their own __init__.py file.
When a module is in the same directory as the script or application
There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.
Example:
If I have a folder, containing script.py and module.py, I can simply import the module by:
import module
The correct way to use relative import is:
from ..Common import foo1
And you also need a __init__.py in all your folders.