Use function from Python script in OS path - python

I have a third-party Python script (foo.py) in a folder that is in my system path (but not the Python sys.path). foo.py is not part of any Python module.
I am writing another script (bar.py) in which I'd like to call a function located in foo.py. Is this possible? Can it be done without explicitly naming the folder of foo.py?
Thanks.

You can include the path of foo.py in the PYTHONPATH environment variable. The interpreter will look also the directories contained there, so you can make the import just like it was on the same directory.

If Python does not find the module, I don't think there's another way then to specify where it can be found, with one easy way being:
import sys
sys.path.append('/myfolder/itsinthisfolder/')
import foo

Related

What is the Python equivalent of Matlab's pathtool adding?

In Python, I use some of my functions so often that I made a file where they are written, no matter the project I am working on. How do I tell Python to add this file to path every time my code is executed so that i can use my functions, just like adding a specific path to Pathtool in Matlab? I know in Python, I could do something like
sys.path.insert(1, 'D:/Python/FunctionDirectory') and then from file_with_functions import my_function but I would have to write this code to all of my projects before start coding to use my functions. I would like to always have the path to my functions added so that I can always use them like in Matlab without worrying about adding them in every single .py file.
you can use PYTHONPATH env var to specify folder where to look for modules
https://www.tutorialspoint.com/What-is-PYTHONPATH-environment-variable-in-Python
To provide complete steps for anyone with my problem for later times, I found my file with functions can be thought of as a module from which functions can be imported just like from file_with_functions import my_function.
But, I needed to add the folder with my module to python path. Messing with environment variables in Windows didn't work for me. Fortunately here, I read about a different solution:
I simply went to my site-packages folder (to find it, import sys then print(sys.path) and look for a name containing the string 'site-packages'). In this folder, I created a new text file and simply pasted the path with my module there:
like this, closed the text file and changed the extension from .txt to .pth (name of file did not matter as long as it was a .pth, Python found it).

How to import python file located in same subdirectory in a pycharm project

I have an input error in pycharm when debugging and running.
My project structure is rooted properly, etc./HW3/. so that HW3 is the root directory.
I have a subfolder in HW3, util, and a file, util/util.py. I have another file in util called run_tests.py.
In run_tests.py, I have the following import structure,
from util.util import my_functions, etc.
This yields an input error, from util.util import load_dataset,proportionate_sample
ImportError: No module named 'util.util'; 'util' is not a package
However, in the exact same project, in another directory (same level as util) called data, I have a file data/data_prep.py, which also imports functions from util/util.py using a similar import statement...and it runs without any problems.
Obviously, I am doing this in the course of doing a homework, so please understand: this is ancillary to the scope of the homework.
The problem goes away when I move the file to another directory. So I guess this question is How do I import a python file located in the same directory in a pycharm project? Because pycharm raises an error if I just do import util and prompts me to use the full name from the root.
Recommended Way:
Make sure to set the working folder as Sources.
You can do it in Pycharm -> Preferences -> Project: XYZ -> Project Structure
Select your working folder and mark it as Sources. Then Pycharm recognize the working folder as a Source folder for the project and you will be able to simply add other files within that folder by using
import filename.py
or
from filename.py import mudule1
=================
Not recommended way:
In Pycharmyou can simply add . before the .py file which you are going to import it from the same folder. In your case it will be
from .util import my_functions
Resource
There is a good reference also for more information with example how to implement Package Relative Imports. I would highly recommend to check this page.
Package Relative Imports
If you don't have an __init__.py create one and add this line
from util.util import my_function
then you can easily import the module in your scripts
the __init__.py tells python that it should treat that folder as a python package, it can also be used to import/load modules too.
in most cases the __init__.py is empty.
Quoting the docs:
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as string, from unintentionally
hiding valid modules that occur later on the module search path. In
the simplest case, __init__.py can just be an empty file, but it can
also execute initialization code for the package or set the __all__
variable, described later.
Right-click on the folder which you want to be marked as the source > Mark Directory as > Source root.
In my case, it worked only when I omit the extension. Example:
import filename
Note: May be a bit unrelated.
I was facing the same issue but I was unable to import a module in the same directory (rather than subdirectory as asked by OP) when running a jupyter notebook (here the directory didn't have __init__.py). Strangely, I had setup python path and interpreter location and everything. None of the other answers helped but changing the directory in python did.
import os
os.chdir(/path/to/your/directory/)
I'm using PyCharm 2017.3 on Ubuntu 16.04
I had the same issue with pycharm, but the actual mistake was that the file I was trying to import didn't have a .py extension, even though I was able to run it as a standalone script. Look in the explorer window and make sure it has a .py extension. If not, right click on the file in the explorer window, pick refactor, and then rename it with a .py extension.
In Pycharm go to "Run - Configuration" and uncheck
'Add Content root to Pythonpath' and
'Add source roots to Pythonpath',
then use
from filename import functionname
For me the issue was, the source directory was marked correctly, but my file to import was named starting with numeric value. Resolved by renaming it.
import 01_MyModuleToImport
to
import MyModuleToImport

How to import a python module from a relative path?

I've got the following file structure:
I'm trying to initialize some objects in main.py that belong to modules in the Listener, Parser and Configurations folders.
I understand I can't just write import listener since it's not in the same path.
What simple ways are there for the imports to work without adding the paths to the PYTHONPATH env variable?
Is there a way to make it work on any machine "out of the box" without the need to add the paths to PYTHONPATH or any solution like that? Preferably something with a relative path like in C++?
Possible duplicate: Importing from a relative path in Python
In short, you need to programmatically define PYTHONPATH in main.py, something like:
import sys, os
sys.path.append(os.path.dirname(__file__))
This implicitly adds current directory to PYTHONPATH. Rest of the part is straightforward
Create __init__.py file in each of the directories. After that, each of the modules can be imported as from Listener import ... or from Misc import ... etc.
The approach works 'out of box' without redefining any environmental variable.

Python pathing to find modules/classes to import

I am new to Python and I am looking to find out how Python discovers the path to modules it imports from. This would be equivalent to CLASSPATH in Java and PERL5LIB in Perl.
E.g. the import block in a script I am looking at looks something like this:
import os
import resource
from localnamespace.localmodule import some_class
I understand that os and resource are native to Python (are part of the core language API) but still the interpreter must have some pointer where to find them. As for localnamespace.localmodule, how do we tell the interpreter where to find this module because it the directory in which this script is does not have a subdirectory named localnamespace.
TLDR
In summary, the search process goes something like:
1) Previously imported in sys.modules?
2) If no, can I find it in the script / interpreter directory?
3) If no, can I find it in any of the directories in the PYTHONPATH environment variable?
4) If no, ImportError
The Longer Answer
Referring to the documentation an import statement first looks at sys.modules which is a dictionary of currently or recently loaded modules.
It it can't find the module there it searches through sys.meta_path - the actual paths here vary between implementations. In general, import paths will be defined in sys.path, which is a list of directories including those in the environment variable PYTHONPATH.
The documentation for sys.path describes itself as:
A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

Python: sys.path.append vs. import?

I have a (single) .py script. In it, I need to import a library.
In order for this library to be found, I need to call sys.path.append. However, I do not want to hardcode the path to the library, but pass it as a parameter.
So my problem is that if I make a function (set_path) in this file, I need to import the file, and import fails because the path is not yet appended.
What are good ways to solve this problem?
Clarification after comments:
I am using IronPython, and the library path is the path to CPython/lib. This path is (potentially) different on every system.
As far as I know, I cannot pass anything via sys.argv, because the script is run in an embedded python interpreter, and there is no main function.
You should not do the import globally, but inside a function which gets called after you appended the path.
Maybe pass the file as an argument using sys.argv, add it to the path and then import it.
Then run your program like this:
python my_program.py somefolder/some_import.py
Here's a reference for using sys.argv: http://www.pythonforbeginners.com/systems-programming/python-sys-argv/

Categories