pythonpath

How do I find out my python path using python?

萝らか妹 提交于 2019-11-26 06:54:56
问题 How do I find out which directories are listed in my system’s PYTHONPATH variable, from within a Python script (or the interactive shell)? 回答1: sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use: import os try: user_paths = os.environ['PYTHONPATH'].split(os.pathsep) except KeyError: user_paths = [] 回答2: You would probably also want this: import sys print(sys.path) Or as a one liner from the terminal: python -c

adding directory to sys.path /PYTHONPATH

*爱你&永不变心* 提交于 2019-11-26 06:28:06
问题 I am trying to import a module from a particular directory. The problem is that if I use sys.path.append(mod_directory) to append the path and then open the python interpreter, the directory mod_directory gets added to the end of the list sys.path. If I export the PYTHONPATH variable before opening the python interpreter, the directory gets added to the start of the list. In the latter case I can import the module but in the former, I cannot. Can somebody explain why this is happening and

PYTHONPATH vs. sys.path

泄露秘密 提交于 2019-11-26 06:04:49
问题 Another developer and I disagree about whether PYTHONPATH or sys.path should be used to allow Python to find a Python package in a user (e.g., development) directory. We have a Python project with a typical directory structure: Project setup.py package __init__.py lib.py script.py In script.py, we need to do import package.lib . When the package is installed in site-packages, script.py can find package.lib . When working from a user directory, however, something else needs to be done. My

Python - add PYTHONPATH during command line module run

大兔子大兔子 提交于 2019-11-26 04:41:41
问题 I want to run: python somescript.py somecommand But, when I run this I need PYTHONPATH to include a certain directory. I can\'t just add it to my environment variables because the directory I want to add changes based on what project I\'m running. Is there a way to alter PYTHONPATH while running a script? Note: I don\'t even have a PYTHONPATH variable, so I don\'t need to worry about appending to it vs overriding it during running of this script. 回答1: For Mac/Linux; PYTHONPATH=/foo/bar/baz

How to properly determine current script directory?

风格不统一 提交于 2019-11-25 22:35:40
问题 I would like to see what is the best way to determine the current script directory in python? I discovered that, due to the many ways of calling python code, it is hard to find a good solution. Here are some problems: __file__ is not defined if the script is executed with exec , execfile __module__ is defined only in modules Use cases: ./myfile.py python myfile.py ./somedir/myfile.py python somedir/myfile.py execfile(\'myfile.py\') (from another script, that can be located in another

Permanently add a directory to PYTHONPATH?

只谈情不闲聊 提交于 2019-11-25 22:26:40
问题 Whenever I use sys.path.append , the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I permanently add a directory to PYTHONPATH ? 回答1: 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)

How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

蹲街弑〆低调 提交于 2019-11-25 22:14:20
问题 I have a directory which hosts all of my Django apps ( C:\\My_Projects ). I want to add this directory to my PYTHONPATH so I can call the apps directly. I tried adding C:\\My_Projects\\; to my Windows Path variable from the Windows GUI ( My Computer > Properties > Advanced System Settings > Environment Variables ). But it still doesn\'t read the coltrane module and generates this error: Error: No module named coltrane 回答1: You know what has worked for me really well on windows. My Computer >