I am working on several projects on the same PyCharm. Like I "attached" them all together. But I recently noticed some weird behaviors. Like when I import a library I haven't installed yet to my script. It shows me a little error as expected. But when I try to install that using python -m pip install my_library, it tells me that it has already installed. I recently noticed that this is because it's using and other pip from another project. I doesn't use the one in the venv folder in the project. Also to run the scripts sometimes it uses python.exe from pythons original directory. It's a whole mess and I have no idea how I can solve it. Sometimes my projects requires different versions of the same library and you can imagine what happens when I change the version.
I make sure each project is using their own interpreter. Don't know what else to do other than this. I am using Python3.6.4 PyCharm2018.3.2 running on Windows10
it sounds like all your projects are configured to use the system's interpreter instead of the virtual environment you set up for each of them.
Follow this instruction to fix it https://www.jetbrains.com/help/pycharm-edu/creating-virtual-environment.html
In terms of using different version of the python library, you can address that by specifying it in requirements.txt file, which you can put in your venv folder for each project. then you can just do pip install -r requirements.txt after you set up your venv. (you need to ensure that the venv is activated - you don't need to worry about this if you have configured the project in PyCharm to use the venv's python interpreter.) You can check this by going to Terminal in your PyCharm and you should see (venv_name) hostusername#host:~/project_folder$
Related
I'm a mobile dev, no experience with backend environment. I'd appreciate some steps to config my Windows to run python projects, like this one: https://github.com/avilash/TikTokAPI-Python
I can't figure out how to config, run, and test those methods that are described. I assume it should be an IDE for python, but not sure which one is good for Windows, and how ton config it to run a project like this.
On mobile it's a lot easier, just download project, import in the IDE and run it. Any best practices for experienced devs are appreciated!
install python on your pc, then do pip3 install -r requirement.txt . I personally prefer vscode. Its the best with required extension installed, or if you are the newbie and you want to learn python, then don't install extension.
Download the standard Python distro from here https://www.python.org/downloads/. I would stick with versions 3.6 or 3.7 for now for stability and compatibility reasons.
Run the installer, the default installation path will be something like C:\Program Files\Python3X\. Since you're installing it for the first time, it will be your only installation, so you can choose during the install to set your environment variables (typically including PATH and PYTHONPATH) to point to this installation as the default one.
Create your project folder anywhere you like, where you can put your .py scripts. You can then cd into the folder in the command line and run any scipt like this python myscript.py (or if you haven't set the default env vars C:\Program Files\Python3X\python.exe myscript.py).
This is the bare minimum you need to be able to run Python projects.
To install the package you mentioned, in the command line type pip install git+https://github.com/avilash/TikTokAPI-Python (or C:\Program Files\Python3X\Scripts\pip.exe install git+https://github.com/avilash/TikTokAPI-Python). This will work because the project has a setup script (setup.py). Note that this will install it into the Python installation folder and you will NOT be able to modify it. If you want to be able to do development on it, clone it into a separate folder, navigate into it and install in 'dev' mode from the current folder using pip install -e . (note the dot!)
For the IDE, you can use VS Code if you're already using it, just install the Python extension. If you're more into IntelliJ, you can then use PyCharm which will have the familiar interface but it's a separate IDE. Both are similarly good in terms of features and maturity.
Other notes you can find useful at the beginning:
If you install multiple Python installation in different places, they are pretty much self-contained and defined by their installation path, but of course the one for which you will select to set the environment vars during installation will be the default one. If you want to run the pip command for a specific installation, then you will have to use C:\Program Files\Python3X\python.exe -m pip install ....
pip is Python package manager, typically you can install anything available on https://pypi.org/ with just pip install pandas for example.
You can check all packages installed using pip list. Since pip itself is also just a Python package, it will always be in the list as it's installed by default when you install Python.
My question is do i have to install django every single time in my virtual environment in order to run my python files? and is this taking up bunch of space on my machine? My project also uses "matplotlib" and every virtual environment i create it also asks me to import the matplotlib module too. its getting annoying. do i have to do this every time?
Im new to Django. I wanted to run some python files in django but they weren't working, so after some research i found out i needed to run my pycharm project in a virtual environment in order to run these python files.
my folders look like this pycharmProjects -> my project
I enter pycharmProjects and I set up virtual environment using "pienv shell". Then i run "python3 manage.py runserver". It turns out i must install django in the virtual environment before the files run.
Short answer is no, you don't have to use a virtual environment at all and can install your dependancies globally instead. However you will soon find that it will cause a lot of issues. The main reason you would create a virtual environment is to give control of your dependancies and prevent bugs that could be caused because of them having their wires crossed between projects.
Short answer yes.
If you create a virualenv you have to install all packages, that your program needs.
Long answer:
You could install django system wide and then create a virtualenv with the option
--system-site-packages then django would be used from your globally installed python.
(Or you install everything just in your global python, put I personally don't think this is good practice)
If you work with many different projects I think you will avoid a lot of trouble if you use one virtualenv per project.
Trouble meaning that one project breaks, because one pip install for another project changed the version of one package and one project can't handle the newer version.
I would recommend to create a requirements.txt file for each project, that lists the dependencies then you can create the virtualenv with following command
pip install -r requirements.txt
if you have requirement.txt files, then you can create virtualenvs rather quickly if going back to an old project and you can delete the virtualenvs whenever you run out of disk space. If you want to be an the safe side, type pip freeze > pipfreeze.txt prior to deleting the virtualenv and use pip install -r pipfreeze.txt if you want to create one with the same modules and the same versions.
You also might want to look at direnv or autoenv if working on a linux like system.
This will automatically switch to the required virtualenv when changing to a project's working directory.
I've been going around but was not able to find a definitive answer...
So here's my question..
I come from javascript background. I'm trying to pickup python now.
In javascript, the basic practice would be to npm install (or use yarn)
This would install some required module in a specific project.
Now, for python, I've figured out that pip install is the module manager.
I can't seem to figure out how to install this specific to a project (like how javascript does it)
Instead, it's all global.. I've found --user flag, but that's not really I'm looking for.
I've come to conclusion that this is just a complete different schema and I shouldn't try to approach as I have when using javascript.
However, I can't really find a good document why this method was favored.
It may be just my problem but I just can't not think about how I'm consistently bloating my pip global folder with modules that I'm only ever gonna use once for some single project.
Thanks.
A.) Anaconda (the simplest) Just download “Anaconda” that contains a lots of python modules pre installed just use them and it also has code editors. You can creat multiple module collections with the GUI.
B.) Venv = virtual environments (if you need something light and specific that contains specific packages for every project
macOS terminal commands:
Install venv
pip install virtualenv
Setup Venve (INSIDE BASE Project folder)
python3 -m venv thenameofyourvirtualenvironment
Start Venve
source thenameofyourvirtualenvironment/bin/activate
Stop Venve
deactivate
while it is activated you can install specific packages ex.:
pip -q install bcrypt
C.) Use “Docker” it is great if you want to go in depth and have a solide experience, but it can get complicated.
Pip is a program used to manage Python distribution. You usually have one system distribution which is by default managed by Pip. When you do pip install scipy, you install package scipy to your system Python. Everytime you try to import scipy after it will work because your system Python has it.
Project specific distributions are acomplished by using virtual environments. python -m venv env or venv env creates a copy of system Python interpreter, pip, setuptools and a couple of other essential tools. In other words, virtual environment created this way is empty.
To use created virtual environement one should use source env/bin/activate. After that, everytime you invoke python command it will use activated Python interpreter. When you install packages using pip, it will install them in the virtual environment rather than to your system python. To use system Python again use deactivate.
Such usage is actually prefered for projects because some user applications could rely on system Python and some packages, and installing, updating etc. could be potentionally dangerous.
Further reading: venv documentation
I have a python library that I am wanting to help out with and fix some issues. I just don't know how to test my changes given the complexity of how python/pip installs libraries.
I have the library installed with pip and I can run python code connecting to the library by doing an "from import *". But now that I want to make changes to it I pulled the code with git and plan to branch to work on my changes. That's fine. I will then do a pull request to merge any changes given tests pass.
But after I make a change, how do I integrate my changes into python to test out the changes I made with the library? Can pip install my custom/modified version of the library?
I have looked around and haven't successfully found an answer to this but perhaps I'm not looking in the right spot.
Can pip install my custom/modified version of the library?
Yes.
There are various ways of approaching this question. A common solution is the use of Python virtual environments. This allows you to create an isolated Python environment that does not share the same packages as your system Python install. You can then install things into it (such as your modified Python library) to test it out.
To get started, you need the virtualenv tool. This is probably available as a package for your distribution, but you can also install it using pip. Once you have it, you can run in the same directory as your code:
virtualenv .venv
This creates a virtuelenv named .venv. You can call it anything you want, but naming it .venv (or anything starting with a .) means it won't clutter up the output of ls in your workspace.
Next, you need to activate the virtualenv:
. .venv/bin/activate.sh
This modifies your $PATH to place the virtualenv at the front of the list of directories. Now when you type python or pip, you'll be using the virtualenv version.
If your code has a setup.py file, you can install it like this:
pip install -e .
The -e means you want to perform an "editable" install, which means python will use the code "in place", and any changes you make will be immediately visible to the code you use for testing.
When you're done, you can run:
deactivate
This will remove the changes that activate made to your environment.
For more information:
Pipenv & Virtual Environments discusses a higher level tool for managing virtual environments.
Virtualenvwrapper is another take on a higher level management tool.
I've been trying to switch my coding to Linux.
I have ironed out most of my issues but one last thing I am not able to find any explanation of is the virtualization of make and bash commands.
I have installed PyCharm which virtualizes everything from what I have seen.
However, when I am cloning repositories from Github, the instructions require building some code using make and then installing them and later on using bash to build dependencies.
I am running the commands in PyCharm terminal but instead of installing into the venv, it's installing the data in /usr/xxx instead.
How do I tell PyCharm to use bash and make in a similar way to pip to virtualize the setup ?
Edit:
One of the projects in question is gym-gazebo which requires:
git clone https://github.com/erlerobot/gym-gazebo/blob/master/INSTALL.md
Then make and make install which installs it in the root
Later on there is also
bash setup_kinetic.bash
Which also uses root folders and not venv
I was able to install it but it is not virtualized the way it should when compared with coding on Windows
Basically the answer based on Evert and CharlesDuffy, what I'm looking for is containerization, since the libraries I'm looking for are C based with a python wrapper (something like that)
Docker, Singularity and Conda are on of the solutions.