This is my current python environment setup on Windows 10:
For the remainder of this post I will assume you have chocolatey installed. Also I'm using the bash as admin terminal from cmder.
Install python3 with choco install python and/or choco install python3. Verify that you have the latest python3 version installed with python --version. Then type choco install pyenv-win. This is the easiest way to do this because it also configures the Environment Variables.
Now install the python version you want with pyenv. In this post I'll go with 3.9.0
pyenv update
pyenv install --list
pyenv install 3.9.0
It seems like no matter what you do, your path will always put /c/Python39/python aka not the pyenv version of python first. Even if you set the pyenv section of your path first by going to My Computer > Properties > Advanced System Settings > Environment Variables and move PYENV to the top of the path, your terminal will always see /c/Python39/python as the default version.
Then I install virtualenvwrapper with the pyenv version of python:
export "PATH=/c/Users/<myusername>/.pyenv/pyenv-win/shims/:$PATH"
/c/Users/<myusername>/.pyenv/pyenv-win/shims/pip install virtualenv
/c/Users/<myusername>/.pyenv/pyenv-win/shims/pip install virtualenvwrapper
source /c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/virtualenvwrapper.sh
export "PATH=/c/Users/<myusername>/.pyenv/pyenv-win/shims/:/c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/:$PATH"
I make a virtual environement like this mkvirtualenv test1. Now the terminal has a (test1) before the prompt so I know that the virtual env is active and working. Before you close out of this terminal, run the command deactivate to stop the virtualenv.
Now when I want to start a python project, I run these commands (Order is really important. If you do the first two commands out of order it won't work until you close and reopen the terminal).
export "PATH=/c/Users/<myusername>/.pyenv/pyenv-win/shims/:/c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/:$PATH"
source /c/Users/<myusername>/.pyenv/pyenv-win/versions/3.9.0/Scripts/virtualenvwrapper.sh
workon test1
And finished! That's how I setup my virtual environments in Python.
See how inconvenient that was lol. On my mac it's easy I just run pyenv virtualenvwrapper then workon test1. So how can I do this better? Is there a simpler way to work with python on Windows 10?
P.S. I would prefer not to use PyCharm. I know a lot of people like it but I'm looking for a more text editor/IDE agnostic solution.
Related
I'm on MacOS, and I have latest python installed via brew. A few months ago, I started using pyenv to be able to switch between latest python and a project that was fixed at python v3.8. I think I got it all set up but I'm confused. pyenv refuses to show me information about system:
$ pyenv versions
* system (set by /Users/<user>/.pyenv/version)
3.8.6
$ pyenv version system
system (set by /Users/<user>/.pyenv/version)
$ cat ~/.pyenv/version
system
How do I get pyenv to show me the version and/or location of system?? Obviously, I can get info about system python when it's the one in use, but why doesn't pyenv show anything about it? Showing info about the current config seems like basic functionality for a config management tool.
By comparison, when I run apt list --installed, it shows me what's installed, whether installed by me or bundled with the OS. It doesn't just show a placeholder for things installed by the system.
So I'm frustrated that pyenv is doing this.
Edit: Wow, nvm is the same way. How? Why? Why do these tools have a built-in disregard for the system config?
Pyenv is a shim tool. It's used to intercept calls to python and pip and transparently give the correct binaries for these commands. When you call python -m some_module, pyenv will find the correct python binary that makes sense for that call, either because it's locally set by a .python-version file or set by the current shell using pyenv shell or pyenv activate commands.
Pyenv tries to not mess up with current python installations, because the environment system may use these tools, and should keep using whatever version they are already using. Thus, it simply packs the currently existing binaries as "system" and does not touch it, so that the existing system prior to installing pyenv keeps working as it was before.
As a best practice, I recommend you to create a new environment and set it with pyenv global to something different of system (e.g. pyenv install 3.9.10 && pyenv virtualenv 3.9.10 myglobal && pyenv global myglobal) -- so that your default application python is different from the operating system python, and thus you can't mess with the system; just leave system as is. If you want to check what system is, you can activate it and call it normally, pyenv activate system ; python --version will show the version, but it's not something you should be doing in the first place, so pyenv doesn't support it.
I had python2 installed in my ubuntu (19.04) and I wanted to get it upgraded to 3.7. I installed the 3.7 but still the version showed as 2.7. but I could run python3 and go to the console and python3.7 executable was available in /usr/bin. Did try all the tricks available in internet without any luck. then I decided to delete python2.7 executable from the /usr/bin and now I cannot even run pip as it tries to find the deleted 2.7 I guess. Getting the following message.
bash: /usr/bin/pip: /usr/bin/python: bad interpreter: No such file or directory
I don't know why it's still trying to find 2.7 like ex girl friend when 3.7 is installed and available in the machine.
Did you set up a path for python 3.7 in your .bashrc file? If not, try that and that should help.
Usually a path is something like:
$ export PATH=$HOME/Nek5000/bin:$PATH
First, run an update to make sure that there aren’t newer versions of the required packages.
sudo apt update --fix-missing
Next, you can try forcing Apt to look for and correct any missing dependencies or broken packages. This will actually install any missing packages and repair existing installs.
sudo apt install -f
(Source)
Also, always remember - never install directly in global environment. You should always create a local environment first(i recommend venv) as there are very high chances you can break something globally.
You can create a local environment by -
python3 -m venv tutorial-env
where tutorial-env is the name of your environment.
To activate this environment, you should run
source tutorial-tutorial-env/bin/activate
I always used Anaconda on Windows so far and could set up an environment while choosing which exact Python to use. E.g. conda create -n myEnvName python=3.7
Now, I want to familiarize with Ubuntu 18.04 LTS and use basic Python environments.
So I followed these steps:
Created folder in my home = ~/.venvPython
(a) I think I already had a 2.7 and 3.6 by default on the OS.
(b) I do not remember for sure, I think I had to do this sudo apt-get install python3-venv.
Created environment this way after CD'ing to .venvPython folder ran this: python3 -m venv venv1BigDataPgm2
source ~/.venvPython/venv1BigDataPgm2/bin/activate
Command python --version says: Python 3.6.9
Running whereis Python shows this:
rohit#rohitUb18043LTS:~$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python2.7-config /usr/bin/python3.6m-config /usr/bin/python /usr/bin/python3.6m /usr/bin/python2.7 /usr/lib/python3.8 /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /etc/python /etc/python2.7 /usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.6 /usr/include/python3.6m /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
My doubts:
Can I specify a Python version directly while creating the environment like with conda?
How do I change this to some other interpreter instead of the 3.6.9?
Do I have to manually install a different Python first, then point it somehow?
Please guide me. Thank you.
Rohit
As far as I can tell the venv standard library appeared in Python 3.3 and was never backported to 2.7.
venv can only create virtual environment for its own version of the interpreter and the virtual environment directory can not be moved to a different location or be renamed. Python 3.foo can not create a virtual environment for Python 3.bar. So it is best to pick the wanted interpreter right from the start.
Since, as shown by the output of whereis python, you seem to already have multiple Python interpreters already installed, you should be able to do something like the following:
$ /path/to/python3.3 -m venv /path/to/my/venvs/venv33
$ /path/to/python3.8 -m venv /path/to/my/venvs/venv38
There seems to be a way to change the Python interpreter associated with a virtual environment (I have not tested it, not sure what the limitations are):
$ /path/to/python3.8 -m venv --upgrade /path/to/my/venvs/venv33
Alternatively use virtualenv which seems to offer a bit more flexibility, but is probably less efficient (its next major release, virtualenv 20, should bring a lot of improvements though).
Ubuntu and other Debian-based systems generally ship whichever Python version was current and deemed sufficiently tested when the release was published; after that, only security updates which preserve the version number but add patches are released (so you might get 3.6.9-123security4 instead of 3.6.9-5 or whatever was current when the release was cut).
If you want to run a specific Python version on one of these platforms, see if you can find an Apt source which provides this version for your system (Ubuntu has a soft underbelly of unofficial PPAs of various repute; Debian has backports) or install it from source yourself. There are add-ons like pyenv which let you do this rather easily, safely, and transparently.
There may also be an existing package which gives you a particular newer version; for example, you can do apt install python3.7 and apt install python3.8 on Ubuntu 18.04, but there are no packages for 3.5 or 3.9. Try apt policy python3.7 to see which specific minor version is available from the Ubuntu package archive.
An alternative to that is to always specify the python version you wish use when running a script.
python3.6 test_script.py
Usually, when I'm on Linux and don't need a specific python3 version, I create native python3 environments.
python3 -m venv myenv
source myenv/bin/activate
But if I need a specific python3 version, I do:
python3.9 -m venv myenv
source myenv/bin/activate
To use a specific python3 version with native environments, you have to install that version using the native package manager (eg. apt).
How can I install pygame on both python 2 and 3 on the same computer?
Its working with python 2 but i cant install it on python 3.
Use virtualenv. It will allow each project to have its own version of python and keeps all its python packages stored next to the project, rather than globally on the system.
cd into your project directory
Install a version of python 2 on your system
$ virtualenv p2env --python=2.7
Install a version of python 3 on your system
$ virtualenv p3env --python=3.5
You can how "activate" either of those environments.
$ ./p2env/bin/activate
You'll see your command prompt gets prefixed with the name of the environment. You're now running under that version of python. Additionally, any packages you pip install will be installed locally for that specific environment.
To deactivate the environment type the following.
$ deactivate
You should notice the environment name is removed from the command prompt. You're now able to switch to a new environment if you wish.
Make sure you add the environment directories virtualenv created to your .gitignore file.
/p2env
/p3env
Of course you can. Most linux distributions do so:
~$ python
python python2 python2.7 python3 python3.4 python3.4m python3m
~$ python
Debian symlinks python to python2.7 so you need to run python3 explicitly. On all other systems you can also have multiple versions in parallel in the same fashion.
Also there is a tool virtualenv which might help you in creating and manageing completely isolated python environments. (https://virtualenv.pypa.io/en/stable/)
I currently have Python 2.6.2 installed on my mac. I am writing a script which MUST run on Python 2.5.2. So I want to write a python script, and test is specifically against 2.5.2 and NOT 2.6.2.
I was looking at virtualenv, but it doesn't seem to solve my problem. I ran python virtualenv.py TEST which made a TEST dir, but it had python 2.6 in it. Is there a way to make virtualenv use a different version of python than what's installed default on my machine? Is another way to use the #! as the first line of the python script? How would I do that?
Check out tox; it's designed to do exactly this.
Here is an example of using tox to run a hello_world.py script against multiple Python versions:
Install tox
pip install tox
Create a tox.ini configuration file
[tox]
envlist = py39, p310, p311
[testenv]
commands = python hello_world.py
The envlist option specifies the Python versions to test against
The commands option specifies the command to run in each environment
Run tox
tox
This will create separate environments for Python 3.9, 3.10, and 3.11 and run the hello_world.py script in each environment.
You can setup a sandboxed environment with different python versions using virtualenv. As Kable has done, install the 2.5. version you want to test against. Then create your virtual environment:
virtualenv --p=python2.5 myapp
To get a clean environment you may use the --no-site-packages switch with the command above. Quite handy when trying to simulate a new, fresh setup. Now activate your virtualenv:
source myapp/bin/activate
If you check the python version you should now get version 2.5.x:
python -V
Now you can install modules as you like into your virtual environment in the usual fashion:
easy_install ...
pip ...
To exit your virtual environment:
deactivate
Hope this may be of help.
You could just install a Python 2.5.2.
I have 3 different versions Python installed on my Lucid and they use different links under /bin/ so it's easy to call the specific version
python -> python3 ->python3.1
python2 -> python2.7
python2.5
try #!/path/to/your/python/version
But make sure you execute the script from the terminal, and make it executable before hand: chmod 755 myscript.py
Using 'virtualenv' you can have different isolated Python environments on a single machine. Also you can switch any-time between the different python interpreter versions.
What is virtualenv?
A Virtual Environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.
How to install?
pip install virtualenv
To create virtual environment for python 2.7 :
root:~# which python2.7
/usr/bin/python2.7
root:~# which python3.4
/usr/local/bin/python3.4
You can also use a Python interpreter of your choice:
root:~# virtualenv -p /usr/bin/python2.7 Vpy27
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /root/Vpy27/bin/python2.7
Also creating executable in /root/Vpy27/bin/python
Installing setuptools, pip, wheel...done.
To begin using the virtual environment, it needs to be activated:
root:~# source Vpy27/bin/activate
The name of the current virtual environment will now appear on the left of the prompt:
(Vpy27) root:~# python -V
Python 2.7.3
Install packages as usual, for example:
(Vpy27) root:~# pip install junos-eznc >> All pip installs done here, will be available only in this environment.
If you are done working in the virtual environment for the moment, you can deactivate it:
(Vpy27) root:~# deactivate
To create virtual environment for python 3.4:
root:~# which python3.4
/usr/local/bin/python3.4
root:~# virtualenv -p /usr/local/bin/python3.4 Vpy34
root:~# source Vpy34/bin/activate
(Vpy34) root:~# python -V
Python 3.4.4
There is also a way to create virtual environment with already available site-packages.