I am trying to install numpy, nltk, etc packages for Python 2 to run a code. But I have Python3 as well and the path variable is set to it. When I try to use any pip install command it shows the package is available in Python3's directory.
Also, I am using VSCode, so I did not add the path variable.
I suggest you use virtual environments. Because if you read about virtual environments, you will find that they are created for such cases.
To create virtual environments, you must do the following:
Make a note of the full file path to the custom version of Python you just installed.
virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
source venv/bin/activate (Linux)
./venv/Scripts/activate.bat (Windows)
Notice how your prompt is now prefixed with the name of your environment (venv, in our case). This is the indicator that venv is currently active, which means the python executable will only use this environment’s packages and settings.
Now run the following:
(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)
now you have access to python2.7.
The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.
Install Pipenv.
$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv (Fedora)
pip install pipenv (Windows)
Creating virtual env with Pipenv.
pipenv install --python 2.7 numpy
This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment. This will avoid the packages version conflicts too.
To activate the environment
pipenv shell
If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.
when we install anything using pip. it will install dependencies for default python version. so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux
Hope this will solve your problem
After crating a virtual environment with python 2.7 you can install your required packages
Related
I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate
I am pretty new to python. Just been working through some online tutorials on udemy. I seem to have an issue with pip installing modules.
I've tried reinstalling them.
Upgrading my python version.
In VS I always just get module not found.
If I do it in the cmd prompt this is what I get below.
You are currently working on the base environment of your computer. For safety, you can first create a new virtual environment with
python3 -m venv -n new_env
So that you won't corrupt any default installations. Then, activate it with
source new_env/bin/activate
And update the pip and setuptools with
pip3 install --upgrade pip
pip3 install --upgrade setuptools
Finally, install numpy via
pip3 install numpy
However, I would recommend using Anaconda to build your virtual environment. When you install Anaconda and make sure it is included in the path of your terminal, all you need to type is
conda create -n new_env python=3.7 numpy
and it will automatically build the wheel for numpy. Here, "new_env" is just an example for a virtual environment name, and Python version 3.7 is also an example.
You can then, activate this conda environment by
conda activate new_env
To use this virtual environment, which you built either with "venv" or "conda", you should locate and activate this environment from the project interpreter settings in VS .
Finally, I would also recommend considering Pycharm IDE which can also help you with creating a virtual environment and installing packages in it.
It seems that you already have the packages installed. Using VS, please, be sure that you selected the correct Python interpreter (https://code.visualstudio.com/docs/python/environments)
I know there are some similar questions,but it is really hard for me to finish it.
I'm trying to create a virtualenv with python 3.7.7 in windows.
I have a downloaded python 3.7.7
C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64
Since I can built a virtualenv using:
python3 -m venv myenv
So I tried to modify it ,so that it can match specific python version:
python3 -m C:\Users\willi\AppData\Local\Programs\Python\python-3.7.8-embed-amd64\python.exe myenv
But it failed:
ModuleNotFoundError: No module named 'C:\\Users\\willi\\AppData\\Local\\Programs\\Python\\python-3')
Any friends can teach me how to build a virtualenv with python 3.7.7?
I think you haven't installed virtual environment in your local python
pip install virtualenv
and follow your steps. Stil, find the error. Try doing below methods, I think you will get your answer-
in command prompt
pip install virtualenv
go to the location, where you want to create your environment
cd location
virtualenv project_env_name
Now, you will find a python env in the desired location, Then go to scripts
cd project_env_name/scripts
activate
You will enter the environment you created. while leaving the environment, do
deactivate
To leave the environment.
This method works if you want to create the same python version environment as python version in your machine.
If you want to create an environment of the different version, you need to install the python of that version.
I installed Anaconda3 so I can create environments and install different packages in each environment. But I fail to understand the difference between the Python in
/usr/bin/python
and
/opt/anaconda3/bin/python
I can seem to access Python 3.6.5 Anaconda from both, why is that? And, what is the difference between both?
Furthermore, I would like to install packages to a single Python environment only.
When you are running python in the terminal, it is looking up your default path to your the python command. In this case, anaconda probably put a line in your shell profile specify the path to the anaconda version, which is why you are seeing it in the interpreter when you run python from either directory.
Secondly, you can set up a conda environment to download app specific dependencies without interfering with your default set up by
conda create --name myenv
source activate myenv
conda install packagename
This will install it in the myenv environment only. To deactivate the environment just run
source deactivate
Here is the documentation on that https://conda.io/docs/user-guide/tasks/manage-environments.html
Judging by your path, you are using Linux which comes with python installed. So /usr/bin/python is default and you have installed the other one later.
For the environments use https://conda.io/docs/user-guide/tasks/manage-environments.html to activate the desired environment, then you can pip install or conda install the packages and it will be places safely only in that environment. Note that spyder icon runs the root environment by default and you have to run it from terminal after activating one of the environments.
Edit:
I'm not sure why you want to use cd to change the python version. I suggest use aliases. I guess you are just changing the path but running the same version of the python anyway. Take a look at this question:
Two versions of python on linux. how to make 2.7 the default
I wanted to create a new virtual environment to install new packages. Following worked for me:
Commands are executed in Jupyter Notebook (OS: Ubuntu 16.04 LTS)
Upgrade pip:
!pip install --upgrade pip
Install virtual environment:
!pip install virtualenv
Select version of Python you want to use in new environment:
I wanted to create an environment with Python version 3. Naming it as Python3_xyz:
!virtualenv -p python3 Python3_xyz
After execution, this will create a folder with the same name in the current working directory (i.e. the location where Jupyter notebook is present)
Create a new option with the name of the created environment
And finally, run the following command:
!python -m ipykernel install --user --name=Python3_xyz
This will create a new option with the name Python3_xyz in the menu from where we create a new notebook.
NOTE: One can run above commands from the terminal as well just don't use '!' before the commands.
This question is bit dated, but since I faced a similar issue, what worked for me might help someone!
I did pip install requests from within my conda environment, but failed to import requests even after trying out everything.
What worked for me: run python -m pip install requests or python3 -m pip install requests within you environment. This installed requests successfully for me.
I used sudo pip install virtualenv, then when I run virtualenv ENV in a directory, I get a Python 2 virtual enviroment.
If I use 'pip3 install virtualenv' to install virtualenv again, will it override the previous installation of virtualenv, then when I run virtualenv ENV, I get a Python 3 virtual enviroment? or will it install a new virtualenv in a different name like virtualenv3 in a different place ?
You don't need to go to those lengths. You can use Python 2's virtualenv to create a Python 3 virtual environment. Supposing you have Python 3's binary installed at /usr/local/bin/python3 then simply run
virtualenv -p /usr/local/bin/python3 ENV
and you will find that
source ENV/bin/activate
gives you the Python 3 environment you want.