sys.path

How do you modify sys.path in Google App Engine (Python)?

Deadly 提交于 2019-11-30 13:24:01
I've tried adding the following line to my handler script (main.py), but it doesn't seem to work: sys.path.append('subdir') subdir lives in the my root directory (i.e. the one containing app.yaml ). This doesn't seem to work, because when I try to import modules that live in subdir , my app explodes. 1) Ensure you have a blank __init__.py file in subdir . 2) Use a full path; something like this: import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) Edit: providing more info to answer questions asked in a comment. As Nick Johnson demonstrates you can place

Why python finds module instead of package if they have the same name?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 07:31:54
问题 Here is my directory structure: /home/dmugtasimov/tmp/name-res root tests __init__.py test_1.py __init__.py classes.py extra.py root.py File contents: root/tests/_ init _.py import os, sys ROOT_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')) if not sys.path or ROOT_DIRECTORY not in sys.path: sys.path.insert(0, ROOT_DIRECTORY) # These imports are required for unittest to find test modules in package properly from root.tests import test_1 root/tests/test_1.py

Why python finds module instead of package if they have the same name?

拈花ヽ惹草 提交于 2019-11-29 04:11:45
Here is my directory structure: /home/dmugtasimov/tmp/name-res root tests __init__.py test_1.py __init__.py classes.py extra.py root.py File contents: root/tests/_ init _.py import os, sys ROOT_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')) if not sys.path or ROOT_DIRECTORY not in sys.path: sys.path.insert(0, ROOT_DIRECTORY) # These imports are required for unittest to find test modules in package properly from root.tests import test_1 root/tests/test_1.py import unittest from root.classes import Class1 class Tests(unittest.TestCase): pass root/_ init _.py -

How to refresh sys.path?

我们两清 提交于 2019-11-28 00:01:56
问题 I've installed some packages during the execution of my script as a user. Those packages were the first user packages, so python didn't add ~/.local/lib/python2.7/site-packages to the sys.path before script run. I want to import those installed packages. But I cannot because they are not in sys.path . How can I refresh sys.path ? I'm using python 2.7. 回答1: As explained in What sets up sys.path with Python, and when? sys.path is populated with the help of builtin site.py module. So you just

Why is my Python 'sys.path' different for each tool I use to run my code?

心不动则不痛 提交于 2019-11-27 21:56:12
问题 I'm confused about the rules used to construct my Python sys.path . I understand that the fist entry will be "the directory containing the script that was used to invoke the Python interpreter", or an empty string if Python was invoked interactively, and that the last entires will be entries based on the installation of Python. But I'm confused about what's going on between these two sets of entries, in particular, how they relate to my PYTHONPATH. When I (A) execute a script that prints out

Add a directory to Python sys.path so that it's included each time I use Python

荒凉一梦 提交于 2019-11-27 20:18:23
Currently, when trying to reference some library code, I'm doing this at the top of my python file: import sys sys.path.append('''C:\code\my-library''') from my-library import my-library Then, my-library will be part of sys.path for as long as the session is active. If I start a new file, I have to remember to include sys.path.append again. I feel like there must be a much better way of doing this. How can I make my-library available to every python script on my windows machine without having to use sys.path.append each time? Simply add this path to your PYTHONPATH environment variable. To do

Python: select one of multiple installed module versions

百般思念 提交于 2019-11-27 19:22:15
On my system, I have several modules installed multiple times. To give an example, numpy 1.6.1 is installed in the standard path at /usr/lib/python2.7/dist-packages , and I have an updated version of numpy 1.8.0 installed at /local/python/lib/python2.7/site-packages/ . The reason I cannot simply remove the old version is that I do not have permissions to change anything on my work computer. I however need to use the new numpy version. I have added /local/python/lib/python2.7/site-packages/ to my PYTHONPATH . Unfortunately, this does not help, since /usr/lib/python2.7/dist-packages is inserted

Permanently adding a file path to sys.path in Python

若如初见. 提交于 2019-11-27 17:40:00
I had a file called example_file.py , which I wanted to use from various other files, so I decided to add example_file.py to sys.path and import this file in another file to use the file. To do so, I ran the following in IPython. import sys sys.path sys.path.append('/path/to/the/example_file.py') print(sys.path) I could see the path I had just added, and when I tried to import this file from another directory path like this: import example_file it worked just fine, but once I came out of IPython, entered it again, and checked the sys.path , I saw that the path which I had added was not present

python: Interplay between lib/site-packages/site.py and lib/site.py

為{幸葍}努か 提交于 2019-11-27 06:48:21
问题 Due to a specific problem which I managed to solve, I spent most of today figuring out how site.py(s) work. There is a point which I don't understand. As far as I understand, when python is loaded, first lib/python2.7/site-packages/site.py is run. It goes over PYTHONPATH , searches for lib/python2.7/site.py , and imports it. This file has the addsitedir method, that not only adds a path to sys.path , but also processes *.pth files which exists on it. At this point, main() from lib/python2.7

Permanently adding a file path to sys.path in Python

为君一笑 提交于 2019-11-26 22:33:12
问题 I had a file called example_file.py , which I wanted to use from various other files, so I decided to add example_file.py to sys.path and import this file in another file to use the file. To do so, I ran the following in IPython. import sys sys.path sys.path.append('/path/to/the/example_file.py') print(sys.path) I could see the path I had just added, and when I tried to import this file from another directory path like this: import example_file it worked just fine, but once I came out of