简体   繁体   English

安装新版本的Python会与旧版本冲突吗?

[英]Will installing new version of Python conflict with old versions

I'm a newbie programmer just installing Python 3.2, but I know I also have an older version of Python on my machine. 我只是安装Python 3.2的新手程序员,但我知道我的机器上也有旧版本的Python。 in fact, I think Macbook comes with it installed. 事实上,我认为Macbook随附了它。 Do I have to worry about having different versions on my computer when I try to start learning Python? 当我尝试开始学习Python时,我是否必须担心计算机上有不同的版本?

For the most part, you don't have to worry about conflicts with system Python. 在大多数情况下,您不必担心与系统Python的冲突。 In fact it is recommended to install a different Python version instead of working with system Python. 实际上,建议安装不同的Python版本,而不是使用系统Python。 Also consider using virtualenv and virtualenvwrapper to maintain any dependencies for each project easily without conflicts. 还要考虑使用virtualenvvirtualenvwrapper轻松维护每个项目的依赖关系而不会发生冲突。

It really depends what OS you're talking about. 这真的取决于你在说什么操作系统。 I'm assuming you're talking about a Mac, since you mentioned Macbook. 我假设你在谈论Mac,因为你提到了Macbook。

Macs come with 2.5 and 2.6 installed as far as I'm aware. 据我所知,Mac安装了2.5和2.6。 At least mine has both those versions, and I've only installed 2.7 manually. 至少我有这两个版本,我只手动安装2.7。

You can check which version of python is the current 'system' python by doing the following in terminal: 你可以通过在终端中执行以下操作来检查哪个版本的python是当前的'system'python:

// check the version of system python
python --version

// tells you where the system version of python is on your PATH
which python

On *nix type Operating Systems, like your Mac, applications aren't really 'installed', like they are in Windows (eliding details). 在* nix类型的操作系统上,与Mac一样,应用程序并未真正“安装”,就像它们在Windows中一样(详见详情)。 Instead, application files are placed in various different parts of the file system. 相反,应用程序文件放在文件系统的各个不同部分。 Python, for example, is placed into the following directory (by default) when installing 2.7: 例如,在安装2.7时,Python会被放置到以下目录中(默认情况下):

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

Since this directory isn't on the system PATH , this version of python won't be used when simply calling python from the command line. 由于此目录不在系统PATH ,因此只需从命令行调用python时就不会使用此版本的python。 The system will search all the folders in the PATH environment variable for an executable file called python. 系统将在PATH环境变量中的所有文件夹中搜索名为python的可执行文件。 It will usually find it in /usr/bin/ or something similar. 它通常会在/usr/bin/或类似的东西中找到它。

To make a new version of Python the 'system' python, you have a couple of options: 要将Python的新版本作为'system'python,你有几个选择:

  1. Modify your .bash_profile, and prepend the path to your new python to the PATH environment variable. 修改.bash_profile,并将新python的路径添加到PATH环境变量中。
  2. symlink the new version of python to a directory already on your PATH like /usr/bin/ 将新版本的python符号链接到PATH上已经存在的目录,例如/ usr / bin /

Be aware that Mac python installers can modify your .bash_profile (in your home directory), to force the new version to be the default system version. 请注意,Mac python安装程序可以修改.bash_profile (在您的主目录中),以强制新版本成为默认系统版本。 This is what my bash_profile shows: 这是我的bash_profile显示的内容:

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

You can happily run multiple versions of python on the same system. 您可以在同一系统上愉快地运行多个版本的python。 A particular version is usually the default though, and that's whatever executable python file is found on the PATH first. 虽然特定版本通常是默认版本,但这是在PATH上首先找到的任何可执行python文件。

If you want to use a different version at any particular point in time, you can: 如果要在任何特定时间点使用其他版本,您可以:

/path/to/python/2.4/python some_script.py
/path/to/python/2.7/python some_script.py
/path/to/python/3.2/python some_script.py

That will execute the script some_script.py under 3 different versions of python. 这将在3个不同版本的python下执行脚本some_script.py Of course, you need to make sure that the /path/to/python is correct. 当然,您需要确保/ path / to / python是正确的。

So yes, you need to be mindful about what version of python you are going to be using, hopefully this will guide you into understanding how applications are installed and which version of an application is launched by default when you don't provide a path. 所以,是的,您需要注意将要使用的python版本,希望这将指导您了解应用程序的安装方式以及默认情况下在您未提供路径时启动的应用程序版本。

Yes, 3.x Python syntax is not backward-compatible with 2.x. 是的,3.x Python语法与2.x不向后兼容。 So if you learn Python 3.x you might not be able to port your knowledge to Python 2.x. 因此,如果您学习Python 3.x,您可能无法将您的知识移植到Python 2.x.

Moreover you should choose if you want to learn 3.x or 2.x. 此外,您应该选择是否要学习3.x或2.x. 2.x is far more widespread than 3.x, but 3.x is where Python is heading. 2.x远远超过3.x,但3.x是Python的标题。 No more innovation will happen in 2.x, and in mid-term most frameworks will be ported to 3.x (right now there are some notable exceptions ) 在2.x中不会再发生创新,并且在中期,大多数框架将被移植到3.x(现在有一些值得注意的例外

Hope that helps! 希望有所帮助!

In general, you should be fine. 一般来说,你应该没事。 Since the Mac is BSD-based, it should maintain the "python" command as pointing to the version that your system requires, which is usually an older version like 2.5. 由于Mac是基于BSD的,因此它应该将“python”命令保持为指向系统所需的版本,这通常是2.5之类的旧版本。 You may have to use a command like python3 to run your Python 3 programs, but other than that it should be transparent to you. 您可能必须使用python3类的python3来运行Python 3程序,但python3 ,它应该对您透明。

As you learn and become more advanced, you can begin using the virtualenv system to maintain separate Python installations for multiple projects. 当您学习并变得更高级时,您可以开始使用virtualenv系统来为多个项目维护单独的Python安装。

Python version with different major or minor version numbers can be installed in parallel. 可以并行安装具有不同主要或次要版本号的Python版本。 For example, you can have 2.4, 2.5, 2.6, 2.7 and 3.1 on the same machine. 例如,您可以在同一台机器上安装2.4,2.5,2.6,2.7和3.1。 However, you can't have versions with the same major and minor number installed at the same time (at least, not without tricks), so you can't have 2.5.2 and 2.5.4 at the same time. 但是,您不能同时安装具有相同主要和次要编号的版本(至少,不是没有技巧),因此您不能同时拥有2.5.2和2.5.4。

Note that you will have to install any third-party libraries once for every Python version. 请注意,您必须为每个Python版本安装一次任何第三方库。

It is very well possible to have multiple versions of python on your machine. 你的机器上有多个版本的python是非常有可能的。 Just make sure, that if you call python in your console it uses the python you want it to use. 请确保,如果您在控制台中调用python ,它将使用您希望它使用的python。 Same goes for your IDE. 您的IDE也是如此。

Regarding the version: It is always nice to have the latest version on board (in python however there are compatibility issues to take into account) , since there might be features you want to use, that are only available with a certain version and upwards. 关于版本:总是很高兴有最新版本(在python中但是有兼容性问题要考虑),因为可能有你想要使用的功能,只有特定版本及以上版本才能使用。 Since this is sometimes tricky to find out, especially if you are new to the field, going with the latest version might be how you should proceed. 因为这有时候很难找到,特别是如果你是新手,那么使用最新版本可能就是你应该如何进行的。

Be careful before installing new version of python. 在安装新版本的python之前要小心。

Python has no backward compatibility. Python没有向后兼容性。

Scripts written for python 2.7.* won't work on python 3 为python 2.7。*编写的脚本不适用于python 3

For example, print "Hello" will work on python 2.7 but not on version3 例如, 打印“Hello”将在python 2.7上运行,但在版本3上不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM