Python: sys.path.append vs. import? - python

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/

Related

How to import some variable in another script without using sys? [duplicate]

This question already has answers here:
How can I import a module dynamically given the full path?
(35 answers)
Closed 1 year ago.
When I try to do using sys in following way, it gives error: name 'path' is not defined.
Here test.py is file in folder directory. Also folder directory has __init__.py in it.
import sys
sys.path.append('c/Users/Downloads/folder')
from test import *
print(path)
Content of test.py
path="sample"
I also tried writing from test import path but it did not work. So is there any other way than sys. I am not able to find why it's not working with it, since most answers recommend this.
EDIT:
Python is used a lot in industry so I am hopeful there must be some way for that (without using or using sys). I can have multiple files with same name in my system so help provided in answer and comments are not permanent fix.
This is an interesting problem so I spend a little time investigating. After some digging, it turns out the problem is there is already another script named "test.py" in one of the sys.path directories and Python is importing that script instead of the one you want.
When you import a module, Python looks for it in each of the search paths in the order they are listed in sys.path and imports the first match. Using sys.path.append('path/to/folder') fails because it adds a search path to the end of the list, and Python already finds a match for test.py in one of the directories listed before it. This is also the reason sys.path.insert(0,'path/to/folder') works - because it inserts the specified path at the front of the list so Python will find it first.
FIX
Change the name of the file 'test.py' to be something unique that won't match any other file names. This is the probably the best way.
You could use sys.path.insert(0,'path/to/folder') which places the specified path first in the list of search paths.
Another way is to go through each of the directories in the sys.path list and remove or rename the other "test.py" file(s). Not recommended
is there any other way than sys
If both the script you are running and the script you want to import are in the same folder, you don't need sys module.
If the files are in different folders and you absolutely won't use sys, then you can set PYTHONPATH in terminal before running the script. Note that you must run the python script from inside this same shell for it to work. Also, this will not work if the folder your script is in also contains a file with the same name as the one you want to import from another folder
Linux Bash:
export PYTHONPATH="path/to/folder/"
Windows Powershell:
set PYTHONPATH="path/to/folder/"

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).

Get 'libs' directory from within Python regardless of Python installation structure

While patching a program I wrote I noticed that I had no safe way to actually get the directory of 'libs', or even 'include' from within the Python installation itself. Every single time I tried to find any python used directory, I just assumed they would always be in the same place, and thus hard-coded them in. Now you might wonder why I am having trouble with this, I mean it's just a simple join call after a sys check, right?
import os, sys
python_base_directory = sys.exec_prefix
libs_directory = os.path.join(python_base_directory, 'libs')
This code works most of the time, but will fail when you are using a virtual environment, or in my case, using travis_ci.
I want to be able to get the path to the 'libs' directory without hard coding anything.

Python Import From Path

I'm using portable-python and haven't been able to find a way of linking .py files to the portable-python interpreter (meaning I can't just double click my scripts to run them). I've been getting around this by dragging scripts into the path portable-python checks and then importing them.
ex:
import random_py_file
but is there a way to substitute the name of the file with its absolute/relative path?
ex:
import C:\file\script.py
This works on linux; however, I am not sure about windows. Essentially what you are doing is adding a path where python will look to find your script.
sys.path.append('C:\file')
import script
Look here to find how to make this permanent.

Use function from Python script in OS path

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

Categories