I thought
我认为
import sys
sys.path.append("/home/me/mydir")
is appending a dir to my pythonpath
将一个dir添加到我的pythonpath中?
if I print sys.path my dir is in there.
如果我打印系统。路径我的目录在那里。
Then I open a new command and it is not there anymore.
然后我打开一个新的命令,它不在那里了。
But somehow Python cant import modules I saved in that dir.
但是Python不能导入我保存在那个目录中的模块。
What Am I doing wrong?
我做错了什么?
I read .profile or .bash_profile will do the trick.
我读取.profile或.bash_profile就可以了。
Do I have to add:
我是否必须加上:
PATH="/Me//Documents/mydir:$PYTHONPATH"
export PATH
To make it work?
让它工作吗?
4 个解决方案
#1
58
Modifications to sys.path
only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH
environment variable:
对系统的修改。路径只适用于Python解释器的生命周期。如果您想要永久执行,需要修改PYTHONPATH环境变量:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
Note that PATH
is the system path for executables, which is completely separate.
注意,PATH是可执行文件的系统路径,它是完全独立的。
**You can write the above in ~/.bash_profile
and the source it using source ~/.bash_profile
**你可以写在~/上面。bash_profile和使用source ~/.bash_profile的源文件
#2
6
Not sure why Matthew's solution didn't work for me (could be that I'm using OSX10.8 or perhaps something to do with macports). But I added the following to the end of the file at ~/.profile
不知道为什么马修的解决方案对我不起作用(可能是我在使用OSX10.8,或者可能与macports有关)。但是我在~/.profile文件的末尾添加了以下内容
export PYTHONPATH=/path/to/dir:$PYTHONPATH
my directory is now on the pythonpath -
我的目录现在在python路径上
my-macbook:~ aidan$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/path/to/dir', ...
and I can import modules from that directory.
我可以从那个目录导入模块。
#3
2
Setting the $PYTHONPATH environment variable does not seem to affect the Spyder IDE's iPython terminals on a Mac. However, Spyder's application menu contains a "PYTHONPATH manager." Adding my path here solved my problem. The "PYTHONPATH manager" is also persistent across application restarts.
设置$PYTHONPATH环境变量似乎不会影响到Spyder IDE在Mac上的iPython终端。但是,Spyder的应用程序菜单包含一个“PYTHONPATH manager”。在这里加上路径就解决了我的问题。“PYTHONPATH manager”在应用程序重新启动时也是持久的。
This is specific to a Mac, because setting the PYTHONPATH environment variable on my Windows PC gives the expected behavior (modules are found) without using the PYTHONPATH manager in Spyder.
这是特定于Mac的,因为在我的Windows PC上设置PYTHONPATH环境变量可以提供预期的行为(找到模块),而无需在Spyder中使用PYTHONPATH管理器。
#4
1
Mathew's answer works for the terminal python shell, but it didn't work for IDLE shell in my case because many versions of python existed before I replaced them all with Python2.7.7. How I solved the problem with IDLE.
Mathew的答案适用于终端python shell,但在我的例子中,它不适用于空闲shell,因为在我用Python2.7.7替换它们之前,有很多版本的python存在。我如何用空闲时间解决问题。
- In terminal,
cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
- 在终端,cd / application /Python\ 2.7/ idlet .app/ content /Resources/
- then
sudo nano idlemain.py
, enter password if required. - 然后sudo纳米idlemain。py,如果需要输入密码。
- after
os.chdir(os.path.expanduser('~/Documents'))
this line, I addedsys.path.append("/Users/admin/Downloads....")
NOTE: replace contents of the quotes with the directory where python module to be added - os.chdir后(os.path.expanduser(~ /文档的)这条线,我添加了sys.path.append(/用户/ admin /下载....)注:引用的内容替换为python模块添加的目录
- to save the change, ctrl+x and enter Now open idle and try to import the python module, no error for me!!!
- 为了保存更改,ctrl+x和enter现在打开空闲,并尝试导入python模块,对我来说没有错误!!!
#1
58
Modifications to sys.path
only apply for the life of that Python interpreter. If you want to do it permanently you need to modify the PYTHONPATH
environment variable:
对系统的修改。路径只适用于Python解释器的生命周期。如果您想要永久执行,需要修改PYTHONPATH环境变量:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
Note that PATH
is the system path for executables, which is completely separate.
注意,PATH是可执行文件的系统路径,它是完全独立的。
**You can write the above in ~/.bash_profile
and the source it using source ~/.bash_profile
**你可以写在~/上面。bash_profile和使用source ~/.bash_profile的源文件
#2
6
Not sure why Matthew's solution didn't work for me (could be that I'm using OSX10.8 or perhaps something to do with macports). But I added the following to the end of the file at ~/.profile
不知道为什么马修的解决方案对我不起作用(可能是我在使用OSX10.8,或者可能与macports有关)。但是我在~/.profile文件的末尾添加了以下内容
export PYTHONPATH=/path/to/dir:$PYTHONPATH
my directory is now on the pythonpath -
我的目录现在在python路径上
my-macbook:~ aidan$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/path/to/dir', ...
and I can import modules from that directory.
我可以从那个目录导入模块。
#3
2
Setting the $PYTHONPATH environment variable does not seem to affect the Spyder IDE's iPython terminals on a Mac. However, Spyder's application menu contains a "PYTHONPATH manager." Adding my path here solved my problem. The "PYTHONPATH manager" is also persistent across application restarts.
设置$PYTHONPATH环境变量似乎不会影响到Spyder IDE在Mac上的iPython终端。但是,Spyder的应用程序菜单包含一个“PYTHONPATH manager”。在这里加上路径就解决了我的问题。“PYTHONPATH manager”在应用程序重新启动时也是持久的。
This is specific to a Mac, because setting the PYTHONPATH environment variable on my Windows PC gives the expected behavior (modules are found) without using the PYTHONPATH manager in Spyder.
这是特定于Mac的,因为在我的Windows PC上设置PYTHONPATH环境变量可以提供预期的行为(找到模块),而无需在Spyder中使用PYTHONPATH管理器。
#4
1
Mathew's answer works for the terminal python shell, but it didn't work for IDLE shell in my case because many versions of python existed before I replaced them all with Python2.7.7. How I solved the problem with IDLE.
Mathew的答案适用于终端python shell,但在我的例子中,它不适用于空闲shell,因为在我用Python2.7.7替换它们之前,有很多版本的python存在。我如何用空闲时间解决问题。
- In terminal,
cd /Applications/Python\ 2.7/IDLE.app/Contents/Resources/
- 在终端,cd / application /Python\ 2.7/ idlet .app/ content /Resources/
- then
sudo nano idlemain.py
, enter password if required. - 然后sudo纳米idlemain。py,如果需要输入密码。
- after
os.chdir(os.path.expanduser('~/Documents'))
this line, I addedsys.path.append("/Users/admin/Downloads....")
NOTE: replace contents of the quotes with the directory where python module to be added - os.chdir后(os.path.expanduser(~ /文档的)这条线,我添加了sys.path.append(/用户/ admin /下载....)注:引用的内容替换为python模块添加的目录
- to save the change, ctrl+x and enter Now open idle and try to import the python module, no error for me!!!
- 为了保存更改,ctrl+x和enter现在打开空闲,并尝试导入python模块,对我来说没有错误!!!